2017-07-19 88 views
-1

我對這個json /數組很新,下面的數據來自一個表,並且我需要將它移植到另一個表中,這需要我根據ID最高的國家進行排序... 在加載的國家是動態... 感激,如果有人可以告訴我,我怎麼能集團的所有國家劃分爲「國家」,並推到我試圖做正確的數據[I]如何迭代和替換JSON

var data = [ 
     {"Product": "DESKTOP", "Type": "APPLE" , "CN": "", "IN": "", "SG": "20"}, 
     {"Product": "DESKTOP", "Type": "DELL" , "CN": "", "IN": "", "SG": "20"}, 
     {"Product": "LAPTOP", "Type": "XXX", "CN": "", "IN": "90", "SG": "28"}, 
     {"Product": "MOBILE", "Type": "XXX","CN": "1","IN": "","SG": "28"}, 
     {"Product": "TABLET", "Type": "XXX","CN": "13","IN": "","SG": "238"} 
    ] 

what_i_need = [ 
    {"Product": "DESKTOP", 
    "Type": "APPLE", 
    "Country": [ 
     {"country": "CN", "id": ""} 
     {"country": "IN", "id": ""} 
     {"country": "SG", "id": "20"} 
    ]}, 
    ... 
    ... 
] 

循環但它結束了像這樣

[{"country": "CN", "id": ""} 
{"country": "IN", "id": ""} 
{"country": "SG", "id": "20"} 
{"country": "CN", "id": ""} 
{"country": "IN", "id": ""} 
{"country": "SG", "id": "20"} 
+1

把你的代碼,來生成陣列,並會幫助您更好地 –

+0

你能分享你的代碼和嘗試? – yashgarg1232

+0

當我試圖生成陣列時,h77和Shiladitya發佈了答案,非常感謝!我希望我能給shiladitya打勾,但是h77拿到了最快的一手牌。不勝感激! – tpb

回答

1

你c使用map函數來更改數組格式。

var data = [{ 
 
    "Product": "DESKTOP", 
 
    "Type": "APPLE", 
 
    "CN": "", 
 
    "IN": "", 
 
    "SG": "20" 
 
    }, 
 
    { 
 
    "Product": "DESKTOP", 
 
    "Type": "DELL", 
 
    "CN": "", 
 
    "IN": "", 
 
    "SG": "20" 
 
    }, 
 
    { 
 
    "Product": "LAPTOP", 
 
    "Type": "XXX", 
 
    "CN": "", 
 
    "IN": "90", 
 
    "SG": "28" 
 
    }, 
 
    { 
 
    "Product": "MOBILE", 
 
    "Type": "XXX", 
 
    "CN": "1", 
 
    "IN": "", 
 
    "SG": "28" 
 
    }, 
 
    { 
 
    "Product": "TABLET", 
 
    "Type": "XXX", 
 
    "CN": "13", 
 
    "IN": "", 
 
    "SG": "238" 
 
    } 
 
]; 
 

 
var fixedKeys = ["Product", "Type"]; 
 
var result = data.map(function(item) { 
 
    var x = { 
 
    Product: item.Product, 
 
    Type: item.Type, 
 
    Country: [] 
 
    }; 
 
    for (var country in item) { 
 
    if (item.hasOwnProperty(country) && !fixedKeys.includes(country)) 
 
     x.Country.push({ 
 
     country: country, 
 
     id: item[country] 
 
     }); 
 
    } 
 
    return x; 
 
}); 
 

 
console.log(result);

+0

謝謝你!你救了我的命! – tpb

+0

不客氣! – H77

0

在這裏,你去與解決方案https://jsfiddle.net/sdhaojsp/

var data = [ 
 
     {"Product": "DESKTOP", "Type": "APPLE" , "CN": "", "IN": "", "SG": "20"}, 
 
     {"Product": "DESKTOP", "Type": "DELL" , "CN": "", "IN": "", "SG": "20"}, 
 
     {"Product": "LAPTOP", "Type": "XXX", "CN": "", "IN": "90", "SG": "28"}, 
 
     {"Product": "MOBILE", "Type": "XXX","CN": "1","IN": "","SG": "28"}, 
 
     {"Product": "TABLET", "Type": "XXX","CN": "13","IN": "","SG": "238"} 
 
    ]; 
 
    var outputData = []; 
 
$.each(data, function(i){ 
 
\t var temp = {}; 
 
    var tempCountry = {}; 
 
\t $.each(data[i], function(key){ 
 
    \t if(key === "Product" || key === "Type"){ 
 
    \t temp[key] = data[i][key]; 
 
    }else{ 
 
    \t tempCountry["country"] = key; 
 
     tempCountry["id"] = data[i][key]; 
 
     if(typeof temp["Country"] === 'undefined') { 
 
     \t temp["Country"] = []; 
 
     } 
 
     \t temp["Country"].push(tempCountry); 
 
     tempCountry = {}; 
 
    } 
 
    }); 
 
    outputData.push(temp); 
 
}); 
 

 
console.log(outputData);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

謝謝你!你救了我的命! – tpb

+0

歡迎@tpb ... – Shiladitya

+0

謝謝!一旦我有足夠的代表,病態的upvote你的答案!謝謝您的幫助 – tpb