I am seeking to extract the desired output from the provided input:
Input Configuration:
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'},
]
};
Desired Output Format:
var outputParams = {
'paramDetail': [
{ 'field' : 'specificationName', 'value' : [ 'strong ', 'weak' ,'energetic']},
{ 'field' : 'incident', 'value' : ['123']},
{ 'field' : 'groupId', 'value' : ['g1', 'group1']},
]
};
The logic implementation I have attempted is:
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);
}
The expected outcome of the code should be:
specificationName ["strong", "weak", "energetic"] incident ["123"] groupid ["g1","group1"]
The requirement states that the data type value
needs to be an array of strings. The end goal is to categorize values based on their respective field names.
Upon execution, the above code does not generate the intended result when parsed.