2015-09-18 22 views
-1

我有JSON數據這種方式與關鍵:reportData和值數組,JSON - 如何通過比較特定鍵的彼此來交換值?

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

誰能幫助我在這個問題?

+1

OP,什麼是'FCOL'在你的例子中,相關性和'ICOL'? – Andy

+0

你有沒有嘗試過任何答案,他們是否爲你工作? –

回答

0

不確定與FCOLICOL數組是你的榜樣作用,但這裏的你可能會解決這個問題的方法之一。

我已經調用函數copyElement,因爲swapJsonKeyValues不起作用。 1)假設你已經解析了數據,它現在是一個JS對象,而不是JSON。2)你不交換鍵/值對,你只是根據條件複製一個元素。

所有函數的作用是檢查每個數組是否至少有14個元素長,以及是否檢查第14個元素是否不是null。如果它將值複製到元素12中,map僅在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

-1

你可以試試這個:

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

地圖回調函數將檢查這些條件,並且將返回所需的數據。

1

假設數據= JSON對象,然後

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