我有这个数组:
var res_data = [
{"id": "1", "text": "AAA", "category": "food", "value": "1"},
{"id": "2", "text": "BBB", "category": "food", "value": "2"},
{"id": "3", "text": "CCC", "category": "drinks", "value": "3"}
];
我想得到这个
{
"food": [
{
"id": "1",
"text": "AAA",
"category": "food",
"value": "1"
},
{
"id": "2",
"text": "BBB",
"category": "food",
"value": "2"
}
],
"drinks": [
{
"id": "3",
"text": "CCC",
"category": "drinks",
"value": "3"
}
]
}
我尝试通过迭代并设置“类别”值 - 作为键,在新数组中,如下所示:
var res = [];
$.each(obj, function () {
if (this.hasOwnProperty("category")) {
res[this['category']] = this;
console.log(this['category']);
}
})
但“食物”指数仍然是最重要的......
drinks:{id: "3", text: "CCC", category: "drinks", value: "3"}
food: {id: "3", text: "CCC", category: "food", value: "2"}
答案 0 :(得分:4)
将数组转换为另一种结构时常见的迭代技术是使用简化:
const arr = [
{"id": "1", "text": "AAA", "category": "food", "value": "1"},
{"id": "2", "text": "BBB", "category": "food", "value": "2"},
{"id": "3", "text": "CCC", "category": "drinks", "value": "3"}
]
const result = arr.reduce((hash, item) => {
if (!hash.hasOwnProperty(item.category)) {
hash[item.category] = []
}
hash[item.category].push(item)
return hash
}, {})
console.log(result)

答案 1 :(得分:2)
var res_data = [
{"id": "1", "text": "AAA", "category": "food", "value": "1"},
{"id": "2", "text": "BBB", "category": "food", "value": "2"},
{"id": "3", "text": "CCC", "category": "drinks", "value": "3"}
];
var result = {};
for(var i = 0; i < res_data.length; i++){
if(result[res_data[i].category] == undefined)
result[res_data[i].category] = [];
result[res_data[i].category].push(res_data[i])
}
console.log(result)
&#13;
答案 2 :(得分:2)
您也可以将forEach
用于数组。
ES5
var arr = [{
"id": "1",
"text": "AAA",
"category": "food",
"value": "1"
},
{
"id": "2",
"text": "BBB",
"category": "food",
"value": "2"
},
{
"id": "3",
"text": "CCC",
"category": "drinks",
"value": "3"
}
],
outpt = {};
arr.forEach(function(item) {
if (!outpt.hasOwnProperty(item.category)) {
outpt[item.category] = []
}
outpt[item.category].push(item)
});
console.log(outpt)
ES6
let arr = [{
"id": "1",
"text": "AAA",
"category": "food",
"value": "1"
},
{
"id": "2",
"text": "BBB",
"category": "food",
"value": "2"
},
{
"id": "3",
"text": "CCC",
"category": "drinks",
"value": "3"
}
],
outpt = {};
arr.forEach((item) => {
if (!outpt.hasOwnProperty(item.category)) {
outpt[item.category] = []
}
outpt[item.category].push(item)
});
console.log(outpt)
答案 3 :(得分:1)
假设输出有效,您可以使用函数reduce
。
var data = [ {"id": "1", "text": "AAA", "category": "food", "value": "1"}, {"id": "2", "text": "BBB", "category": "food", "value": "2"}, {"id": "3", "text": "CCC", "category": "drinks", "value": "3"}],
result = data.reduce((a, c) => {
(a[c.category] || (a[c.category] = [])).push(c);
return a;
}, {});
console.log(result);
答案 4 :(得分:0)
试试这个:
var res_data = [
{"id": "1", "text": "AAA", "category": "food", "value": "1"},
{"id": "2", "text": "BBB", "category": "food", "value": "2"},
{"id": "3", "text": "CCC", "category": "drinks", "value": "3"}
];
function filter(arr, key, value) {
var res = [];
for(var x = 0, max = arr.length; x < max; x = x + 1) {
if(typeof arr[x] === 'object' && arr[x].hasOwnProperty(key) && arr[x][key] === value) {
res.push(arr[x]);
}
}
return res;
}
然后你可以按照以下任何属性进行过滤:
filter(res_data, 'category', 'food');
它将返回:
[
{"id": "1", "text": "AAA", "category": "food", "value": "1"},
{"id": "2", "text": "BBB", "category": "food", "value": "2"}
]