JSON - 如何通过比较特定键来交换值?

JSON - How to interchange values by comparing each other of particular keys?

我以这种方式获得 JSON 数据 key:reportData 和值数组,

 {"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"]]}

我想比较和交换每个数组元素中的数据:特别是第 12 和第 14 个索引。

ex: in "reportData":[
    ["1185","R","4t","G","06","L","GT","04309","2546","2015","CF FE","01H1","20","23840","FF20"]]

即,我想使用此逻辑将“20”与 'FF20' 进行比较和互换。

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.

并且必须对 "reportData" 键中的所有数组列表重复此操作。

所以,我的最终 JSON 会是这样,

    "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

我试过这种方式,但它是互换的,

function swapJsonKeyValues(input) {
        var one=['FCOL,ICOL']; output = {};
        for (one in input) {
            if (input.hasOwnProperty(one)) {
                output[input[one]] = one;
            }
        }
        return output;
    }

谁能帮我解决这个问题?

你可以试试这个:

var jsonData =  {"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"]]}

jsonData.reportData.map(function (row) {return row[12] = (row[14]) ? row[14] : row[12];})
console.log(jsonData);

地图回调函数将检查这些条件并return所需的数据。

假设数据 = json 对象,那么

data.reportData.forEach(function(row){
    if(row[14] != null)
        row[12] = row[14];
});

不确定带有 FCOLICOL 的数组在您的示例函数中的用途,但这是您可能解决此问题的一种方法。

我调用函数 copyElement 因为 swapJsonKeyValues 不起作用。 1) 假设你已经解析了数据,它现在是一个 JS 对象,而不是 JSON 2) 你没有交换 key/value 对,你只是根据条件复制一个元素。

该函数所做的只是检查每个数组是否至少有 14 个元素长,如果是,则检查第 14 个元素是否不是 null。如果它是将值复制到元素 12。map 只需 returns 回调后的新数组 运行 在 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);

DEMO