trying to create an array of arrays for return JSON

261 views Asked by At

Im currently working on this script that is written in javascript that returns data from the netsuite ERP platform.

Right now we have the code returning in an array, whilst this is good, it is a result of a dataset of product information.

The script is querying for 3 products, and as a result it returns an array of 21 keys. this should be returning 3 arrays of arrays so we can handle the content easily externally to Netsuite.

I for the life of me cant figure out which loop I am required to create a new array to manage the content.

function loadRecord(request, response)
{
    var recType = request.getParameter('recType');
    var savedSearchId = request.getParameter('savedSearchId');
    var internalid = request.getParameter('internalid');
    //perform the required search.

    var filter = [];

    if(recType == 'customer' || recType == 'contact' )
    {
        filter[0] = new nlobjSearchFilter('internalid', null, 'is', internalid); // just get the 1 item by the internal id of the record
    }

    if( recType == 'item')
    {
        var internal_ids = new Array();
         internal_ids[0] = 25880;
         internal_ids[1] = 25980;
         internal_ids[2] = 333 ;
        filter[0] = new nlobjSearchFilter('internalid', null, 'anyOf', internal_ids); // just get the 1 item by the internal id of the record       
    }


    if(recType == 'transaction')
    {       
        filter[0] = new nlobjSearchFilter('type',null,'anyOf','SalesOrd');
        filter[1] = new nlobjSearchFilter('internalid','customer','is', internalid );
    }


    var rsResults = nlapiSearchRecord(recType, savedSearchId, filter);
    var rsObj = [];



     // not sure how to make each row a new array of arrays so it is structured more elegantly...
    for (x = 0; x < rsResults.length; x++)
    {
        var flds = rsResults[x].getAllColumns();

        for (i = 0; i < flds.length; i++)
        {

            var rowObj = {};
            rowObj.name = flds[i].getName();
            rowObj.label = flds[i].getLabel();
            rowObj.val = rsResults[x].getValue(flds[i].getName(), flds[i].getJoin(), flds[i].getSummary());
            rowObj.txtval = rsResults[x].getText(flds[i].getName(), flds[i].getJoin(), flds[i].getSummary())            
            rsObj.push(rowObj);
        }
    }
    response.write(JSON.stringify(rsObj));
}

Any help greatly appreciated

3

There are 3 answers

0
philwills On

Is this what you're looking for?

var rsObj = [];
var rowArr, fields, x, i;

for (x = 0; x < rsResults.length; x++)
{
    flds = rsResults[x].getAllColumns();

    for (i = 0; i < flds.length; i++)
    {

        rowArr = rsObj[x] = [];
        rowArr.push(flds[i].getName());
        rowArr.push(flds[i].getLabel());
        rowArr.push(rsResults[x].getValue(flds[i].getName(), flds[i].getJoin(), flds[i].getSummary()));
        rowArr.push(rsResults[x].getText(flds[i].getName(), flds[i].getJoin(), flds[i].getSummary()));
    }
}

console.log(rsObj[0][0]); // row0.name
console.log(rsObj[2][1]); // row2.label
0
Basti On

Maybe something like this:

for (var x = 0; x < rsResults.length; x++)
{
    var flds = rsResults[x].getAllColumns();

    for (var i = 0; i < flds.length; i++)
    {
        rsObj.push({
            name: flds[i].getName(),
            label: flds[i].getLabel(),
            val: rsResults[x].getValue(flds[i].getName(), flds[i].getJoin(), flds[i].getSummary()),
            txtval: rsResults[x].getText(flds[i].getName(), flds[i].getJoin(), flds[i].getSummary())
        });
    }
}

If you are using ECMAScript5, you cold simplify the loop with forEach, like this:

rsResults.forEach(function(result) {
    result.getAllColumns().forEach(function(fields) {
        rsObj.push({
            name: fields.getName(),
            label: fields.getLabel(),
            val: result.getValue(fields.getName(), fields.getJoin(), fields.getSummary()),
            txtval: result.getText(fields.getName(), fields.getJoin(), fields.getSummary())
        });
    });
});
0
Prabhat Jain On

This must solve you problem. Some declaration problem might be there.

var rsObj = [];

for (int x = 0; x < rsResults.length; x++)
{
    var flds = rsResults[x].getAllColumns();

    for (int i = 0; i < flds.length; i++)
    {

        var rowObj = [];
        rowObj.push(flds[i].getName());
        rowObj.push(flds[i].getLabel());
        rowObj.push(rsResults[x].getValue(flds[i].getName(), flds[i].getJoin(), flds[i].getSummary()));
        rowObj.push(rsResults[x].getText(flds[i].getName(), flds[i].getJoin(), flds[i].getSummary()));            

        rsObj.push(rowObj);
    }
}