I am having JSON data this way with key:reportData and array of values,
{"reportData":[
["1185","R","4t","G","06","L","GT","04309","2546","2015","CF FE","01H1","20","23840","FF20"],
["1186","R","5t","R","01","L","TP","00110","1854","2016","FE LL","06W3","01","19065","FB01"],
["1187","R","6t","H","06","L","TP","04333","1864","2015","CF FE SL","0209","FD22","19845",null],
["1188","R","7t","H","06","L","PR","04041","6951","2015","CC CT FE GN PC","0070","00","36590","LB00"],
["1189","R","8t","H","06","L","WS","04290","4450","2014","CF EN FE PC TP","0070","EA30","28320.00",null],
["1190","R","9t","H","06","L","LA","04915","4430","2015","CF DK FE RR TC","0040","10","23680","FB10"],
["1191","R","10t","H","06","L","LF","04335","2532","2015","CF FE GE","0040","FC10","22970",null],
["1192","R","11t","H","06","L","SA","04772","8345","2015","BZ C8 FE","01D6","13","33390","LC13"]]}
I want to compare and interchange data in each array element : particularly 12th and 14th indexes.
ex: in "reportData":[
["1185","R","4t","G","06","L","GT","04309","2546","2015","CF FE","01H1","20","23840","FF20"]]
ie, I want to compare and interchange '20' with 'FF20' using this logic .
If 14th index value != null then assign,
12th index=14th index value.
else if 14th index value ==null,
then leave 12th index=12th index value as it is.
And this has to be repeated for all the list of arrays in the "reportData" key.
So, my final JSON would be this way,
"reportData":[
["1185","R","4t","G","06","L","GT","04309","2546","2015","CF FE","01H1","FF20","23840","FF20"],//interchange 12th with 14th as 14th !=null
["1186","R","5t","R","01","L","TP","00110","1854","2016","FE LL","06W3","FB01","19065","FB01"],//interchange 12th with 14th as 14th !=null
["1187","R","6t","H","06","L","TP","04333","1864","2015","CF FE SL","0209","FD22","19845",null],//leave 12th as IT IS as 14th ==null
["1188","R","7t","H","06","L","PR","04041","6951","2015","CC CT FE GN PC","0070","00","36590","LB00"],//interchange 12th with 14th as 14th !=null
["1189","R","8t","H","06","L","WS","04290","4450","2014","CF EN FE PC TP","0070","EA30","28320.00",null],//leave 12th as IT IS as 14th ==null
["1190","R","9t","H","06","L","LA","04915","4430","2015","CF DK FE RR TC","0040","10","23680","FB10"],//interchange 12th with 14th as 14th !=null
["1191","R","10t","H","06","L","LF","04335","2532","2015","CF FE GE","0040","FC10","22970",null],//leave 12th as IT IS as 14th ==null
["1192","R","11t","H","06","L","SA","04772","8345","2015","BZ C8 FE","01D6","13","33390","LC13"]]//interchange 12th with 14th as 14th !=null
I tried this way but it is interchanging ,
function swapJsonKeyValues(input) {
var one=['FCOL,ICOL']; output = {};
for (one in input) {
if (input.hasOwnProperty(one)) {
output[input[one]] = one;
}
}
return output;
}
Can anyone help me in this issue?
Not sure what the array with FCOL
and ICOL
are for in your example function, but here's one way you might approach this problem.
I've called the function copyElement
because swapJsonKeyValues
doesn't work. 1) Assuming you've already parsed the data it's now a JS object, not JSON 2) you're not swapping key/value pairs, you're simply copying an element based on a condition.
All the function does is check that the each array is at least 14 elements long, and if it is checks to see if the 14th element isn't null
. If it is copy the value to element 12. map
simply returns a new array after the callback has been run on each array in reportData
.
function copyElement(obj) {
return obj.reportData.map(function (el) {
if (el.length >= 14 && el[14] !== null) el[12] = el[14];
return el;
});
}
copyElement(obj);