尝试创建一个返回JSON的数组数组

时间:2013-12-11 05:55:10

标签: javascript arrays json loops stringify

我目前正在处理这个用javascript编写的脚本,该脚本从netsuite ERP平台返回数据。

现在我们将代码返回到数组中,虽然这很好,但它是产品信息数据集的结果。

该脚本正在查询3个产品,因此它返回一个包含21个键的数组。 这应该返回3个数组数组,这样我们就可以在Netsuite外部轻松处理内容。

我为我的生活找不到我需要创建一个新数组来管理内容的循环。

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));
}

任何帮助非常感谢

3 个答案:

答案 0 :(得分:0)

这是你要找的吗?

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

答案 1 :(得分:0)

也许是这样的:

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())
        });
    }
}

如果您正在使用ECMAScript5,那么您可以使用forEach来简化循环,如下所示:

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())
        });
    });
});

答案 2 :(得分:0)

这必须解决你的问题。可能存在一些声明问题。

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);
    }
}