2017-07-19 50 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