从另一个创建一个对象

时间:2014-07-08 19:36:29

标签: javascript

我需要从以下输入中获取输出:

输入格式:

var inputParams = { 
    'inputDetails' :[
        { 'field' : 'specificationName', 'value' : 'strong'},
        { 'field' : 'specificationName', 'value' : 'weak'},
        { 'field' : 'specificationName', 'value' : 'energetic'}
        { 'field' : 'incident', 'value' : '123'},
        { 'field' : 'groupId', 'value' : 'g1'},
        { 'field' : 'groupId', 'value' : 'group1'},
    ]
};

输出格式:

var outputParams = {
    'paramDetail': [
        { 'field' : 'specificationName', 'value' : [ 'strong ', 'weak' ,'energetic']},
        { 'field' : 'incident', 'value' : ['123']},
        { 'field' : 'groupId', 'value' : ['g1', 'group1']},
    ] 
};

我尝试的逻辑是:

var changedList = {
                   changedJsonObject : []
                  };
var i = 0 ;
var prev;
var firstTime = true;
var index = 0;
var facetfields = ['strong', 'weak' ,'energetic'];
do {
    if (!params[index].field.localeCompare(facetFields[i])) {
        if (prev == null) {
            prev = params[index].field;
        }
        console.log(index + " " + params[index].field + " " + params[index].value);
        if (!prev.localeCompare(params[index].field)) {
            if (firstTime) {
                console.log("create");
                outputParams.paramDetail.push({
                    "field": params[index].field,
                    "value": [params[index].value]
                });
                firstTime = false;
            } else {
                console.log("update");
                for (var tempInd = 0; tempInd < outputParams.paramDetail.length; tempInd++) {
                    if (!outputParams.paramDetail[tempInd].field.localeCompare
                         (params[index].field)) {
                        outputParams.paramDetail[tempInd].value =
                            outputParams.paramDetail[tempInd].value + "," + params[index].value;
                        break;
                    }
                }
            }
        }
    } else {
        i++;
        index = index - 1;
        firstTime = true;
        prev = null;
    }
}
index++;
}
while (index < params.length);
for (var s in outputParams.paramDetail) {
    console.log(outputParams.paramDetail[s].field);
    console.log(outputParams.paramDetail[s].value);
}

上述代码的输出应为:

specificationName [&#34;强大&#34;,&#34;弱&#34;,&#34;精力充沛&#34;] 事件 [&#34; 123&#34;] GROUPID [&#34; G1&#34;&#34;组1&#34;]

要求是类型value应该是一个字符串数组。我的最终目标是根据字段名称对值进行分组。

上面的代码给了我一个结果,当我解析时不是一个合适的结果。

4 个答案:

答案 0 :(得分:0)

仅当对象排序

时才会起作用
var outputparams = { 'paramDetail': [] };
var lastGroup = "";

for ( var n = 0; n < inputParams.inputDetails.length; n++ )
{
     if ( lastGroup !=  inputParams.inputDetails[n].field )
     {
         outputparams.paramDetail.push({ 'field': inputParams.inputDetails[n].field, 'value': [] });
         lastGroup = inputParams.inputDetails[n].field;
     }
     var currentField = outputparams.paramDetail.length -1;

     outputparams.paramDetail[currentField].value.push(inputParams.inputDetails[n].value);

}

目前尚未经过测试,但这就是我要开始的。

答案 1 :(得分:0)

试试这个

var inputParams = { 
    "inputDetails" : [
        { "field" : "specificationName", "value" : "strong"},
        { "field" : "specificationName", "value" : "weak"},
        { "field" : "specificationName", "value" : "energetic"},
        { "field" : "incident", "value" : "123"},
        { "field" : "groupId", "value" : "g1"},
        { "field" : "groupId", "value" : "group1"}
    ]
};

var outputParams = {
    "paramDetail" : []
};

for(i=0;i<inputParams.inputDetails.length;i++){
    exist = false;
    for(j=0;j<outputParams.paramDetail.length;j++){
        if(outputParams.paramDetail[j].field == inputParams.inputDetails[i].field){
            outputParams.paramDetail[j].value.push(inputParams.inputDetails[i].value);
            exist = true;
            break;
        }
    }
    if(!exist){
        outputParams.paramDetail.push({"field" : inputParams.inputDetails[i].field, "value" : [inputParams.inputDetails[i].value]});
    }
}

console.log(outputParams);

答案 2 :(得分:0)

感谢Valijon使用javascript来更新数组值。

刚刚改变了Valijon给出的更新行。我的代码按预期工作。

outputParams.paramDetail[tempInd].value.push(arr.jsonList1[index].value);

答案 3 :(得分:0)

尝试使用以下函数来处理inputParams

var process = function (input) {
    var results = {};
    input.inputDetails.forEach(function (value, key) {
        if (!(value.field in results)) results[value.field] = [];
        results[value.field].push(value.value);
    });
    var output = { paramDetail : [] };
    for (key in results) {
        output.paramDetail.push({
            field: key,
            value: results[key]
        });
    }
    return output;
};

var outputParams = process(inputParams);