[英]JSON filter using javascript
我有一個包含重復數據的JSON對象。 但是這些數據可以更改,但是鑒於此結構,我需要根據國家/地區組織此JSON
請找到原始的JSON
{
"dataList": [
{
"country": "AUS",
"Cart": 1610950,
"orders": 35670,
"Viewed": 966570,
"Visits": 32190
},
{
"country": "AUS",
"Cart": 1610950,
"orders": 35670,
"Viewed": 966570,
"Visits ": 32190
},
{
"country": "AUS",
"Cart": 1610950,
"orders": 35670,
"Viewed": 966570,
"Visits": 32190
},
{
"country": "AUS",
"Cart": 1610950,
"orders": 35670,
"Viewed": 966570,
"Visits": 32190
},
{
"country": "US",
"Cart": 1610950,
"orders": 35670,
"Viewed": 966570,
"Visits": 32190
},
{
"country": "US",
"Cart": 1610950,
"orders": 35670,
"Viewed": 966570,
"Visits": 32190
},
{
"country": "US",
"Cart": 1610950,
"orders": 35670,
"Viewed": 966570,
"Visits": 32190
},
{
"country": "US",
"Cart": 1610950,
"orders": 35670,
"Viewed": 966570,
"Visits": 32190
},
{
"country": "US",
"Cart": 1610950,
"orders": 35670,
"Viewed": 966570,
"Visits": 32190
},
{
"country": "CANADA",
"Cart": 1610950,
"orders": 35670,
"Viewed": 966570,
"Visits": 32190
},
{
"country": "CANADA",
"Cart": 1610950,
"orders": 35670,
"Viewed": 966570,
"Visits": 32190
},
{
"country": "CANADA",
"Cart": 1610950,
"orders": 35670,
"Viewed": 966570,
"Visits": 32190
},
{
"country": "CANADA",
"Cart": 1610950,
"orders": 35670,
"Viewed": 966570,
"Visits": 32190
},
{
"country": "CANADA",
"Cart": 1610950,
"orders": 35670,
"Viewed": 966570,
"Visits": 32190
},
{
"country": "CANADA",
"Cart": 1610950,
"orders": 35670,
"Viewed": 966570,
"Visits": 32190
}
]
}
JSON更新
我需要將這些JSON組織成更有意義的形式,例如
{
"dataList":
[
{
"country" : "AUS",
"data" :
[
{
"Cart" : 1610950,"orders" : 35670,"Viewed" : 966570,"Visits" : 32190
},
{
"Cart" : 1610950, "orders" : 35670, "Viewed" : 966570, "Visits" : 32190
},
{
"Cart" : 1610950,"orders" : 35670,"Viewed" : 966570,"Visits" : 32190
},
{
"Cart" : 1610950,"orders" : 35670,"Viewed" : 966570,"Visits" : 32190
},
{
"Cart" : 1610950,"orders" : 35670,"Viewed" : 966570,"Visits" : 32190
}
]
},
{
"country" : "CANADA",
"data" :
[
{
"Cart" : 1610950,"orders" : 35670,"Viewed" : 966570,"Visits" : 32190
},
{
"Cart" : 1610950,"orders" : 35670,"Viewed" : 966570,"Visits" : 32190
},
{
"Cart" : 1610950,"orders" : 35670,"Viewed" : 966570,"Visits" : 32190
},
{
"Cart" : 1610950,"orders" : 35670,"Viewed" : 966570,"Visits" : 32190
},
{
"Cart" : 1610950,"orders" : 35670,"Viewed" : 966570,"Visits" : 32190
}
]
},
{
"country" : "US",
"data" :
[
{
"Cart" : 1610950,"orders" : 35670,"Viewed" : 966570,"Visits" : 32190
},
{
"Cart" : 1610950,"orders" : 35670,"Viewed" : 966570,"Visits" : 32190
},
{
"Cart" : 1610950,"orders" : 35670,"Viewed" : 966570,"Visits" : 32190
},
{
"Cart" : 1610950,"orders" : 35670,"Viewed" : 966570,"Visits" : 32190
},
{
"Cart" : 1610950,"orders" : 35670,"Viewed" : 966570,"Visits" : 32190
}
]
}
]
}
無論如何,有使用JavaScript或jquery動態地將上述JSON轉換為以下JSON的代碼,下面的代碼是將2個不同的數組映射為鍵值對的。 但我認為需要擴大。
$.formatToJSON = function(datatest) {
var array = {};
var list = {};
list.dataSet = [];
var len = datatest.columnHeaders.length;
var rows = datatest.rows.length;
// log("Column Length : "+len);
// log("Row Length : "+rows);
var j = 0;
while (j != rows) {
var i = 0;
var array = {};
while (i != len) {
var name = datatest.columnHeaders[i].name;
var pair = datatest.rows[j][i];
// log( name + " : "+pair);
array[name] = pair;
i += 1;
}
list.dataSet.push(array);
j += 1;
}
/*log(list);*/
return list;
return this;
};
一旦您要解析原始的JSON(如果您很掙扎,演示中就會有一個副本),此函數將創建新的結構:
function reorder(obj) {
var out = { dataList: [] };
var objs = obj.dataList;
var tmp = {};
for (var i = 0, l = objs.length; i < l; i++) {
var clone = JSON.parse(JSON.stringify(objs[i]));
var country = clone.country;
if (!tmp[country]) { tmp[country] = []; }
delete clone.country;
tmp[country].push(clone);
out.dataList.push(tmp);
}
return out;
}
reorder(obj);
請注意,我對數據的結構進行了一些更改,因為我認為它更有意義。 不用擁有country
屬性,只需使用對象鍵作為國家/地區名稱即可。 它使處理imo變得容易一些。
例如,您可以執行Object.keys(obj.dataList[0]);
無需遍歷數據結構即可獲得所有活躍國家的清單。
var formatedData = new Array();
$(data.dataList).each(function(){
var currentData = this;
var detail = {"Cart": currentData.Cart,
"orders": currentData.orders,
"Viewed": currentData.Viewed,
"Visits": currentData.Visits};
var newData = jQuery.grep(formatedData,function(a){
return a.country == currentData.country;
});
if(newData.length==0){
newData = {country: this.country, data: [detail]};
formatedData.push(newData);
}
else{
newData[0].data.push(detail);
}
});
console.log(formatedData);
您可以使用reduce函數,例如
data.dataList.reduce(function(acc, el){
var cur = acc.keys[el.country];
if(!cur){
acc.keys[el.country] = cur = {country:el.country, data:[]};
acc.result.push(cur);
}
cur.data.push({
Cart: el.Cart, orders: el.orders, Viewed:el.Viewed, Visits:el.Visits
});
return acc;
}, {keys:{}, result:[]});
var data = { "dataList": [{ "country": "AUS", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "AUS", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "AUS", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "AUS", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "US", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "US", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "US", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "US", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "US", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "CANADA", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "CANADA", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "CANADA", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "CANADA", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "CANADA", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "CANADA", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }] }; document.getElementById('before').innerHTML = JSON.stringify(data, null, 2); data.dataList = data.dataList.reduce(function(acc, el) { var cur = acc.keys[el.country]; if (!cur) { acc.keys[el.country] = cur = { country: el.country, data: [] }; acc.result.push(cur); } console.log(el, el.Visits); cur.data.push({ Cart: el.Cart, orders: el.orders, Viewed: el.Viewed, Visits: el.Visits }); return acc; }, { keys: {}, result: [] }).result; document.getElementById('res').innerHTML = JSON.stringify(data, null, 2);
<div>Before:</div> <pre id="before"></pre> <div>Result:</div> <pre id="res"></pre>
嘗試
var res = {
"dataList": []
};
var arr = $.map(data.dataList, function(value, key) {
var fn = function(_arr) {
return $.map(_arr, function(v, k) {
var obj = {};
if (k !== "country") {
obj[k] = v
};
return k !== "country" ? obj : null
})
};
if (!res.dataList.length || value.country === res.dataList[0].country) {
if (res.dataList.length === 0) {
res.dataList.push({
country: value.country,
data: []
});
} else {
res.dataList[0].data = fn(value)
}
} else {
if (res.dataList.length > 0 && res.dataList.every(function(v) {
return value.country !== v.country
})) {
res.dataList.push({
country: value.country,
data: fn(value)
});
}
};
});
var data = { "dataList": [{ "country": "AUS", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "AUS", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "AUS", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "AUS", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "US", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "US", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "US", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "US", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "US", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "CANADA", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "CANADA", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "CANADA", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "CANADA", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "CANADA", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }, { "country": "CANADA", "Cart": 1610950, "orders": 35670, "Viewed": 966570, "Visits": 32190 }] }; var res = { "dataList": [] }; var arr = $.map(data.dataList, function(value, key) { var fn = function(_arr) { return $.map(_arr, function(v, k) { var obj = {}; if (k !== "country") { obj[k] = v }; return k !== "country" ? obj : null }) }; if (!res.dataList.length || value.country === res.dataList[0].country) { if (res.dataList.length === 0) { res.dataList.push({ country: value.country, data: [] }); } else { res.dataList[0].data = fn(value) } } else { if (res.dataList.length > 0 && res.dataList.every(function(v) { return value.country !== v.country })) { res.dataList.push({ country: value.country, data: fn(value) }); } }; }); $("pre").text(JSON.stringify(res, null, 2)); console.log(res)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <pre></pre>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.