This question already has an answer here:
这个问题在这里已有答案:
- Group array items using object 10 answers
使用对象10答案的组数组项
What would be an elegant way (jQuery or JavaScript) to merge such an array of objects:
合并这样一个对象数组的优雅方式(jQuery或JavaScript):
var params = [
{"name":"filter_color","value":"black"},
{"name":"filter_color","value":"blue"},
{"name":"filter_size","value":"l"},
{"name":"filter_size","value":"m"}
];
into:
var params = [
{"name":"filter_color","value":"black,blue"},
{"name":"filter_size","value":"l,m"}
]
for later use in $.param(params)?
以后在$ .param(params)中使用?
2 个解决方案
#1
0
You could use a hash table and group it by the wanted key.
您可以使用哈希表并按所需键对其进行分组。
function groupBy(array, key, fn) {
var hash = Object.create(null);
return array.reduce(function (r, o) {
if (!hash[o[key]]) {
hash[o[key]] = {};
hash[o[key]][key] = o[key];
r.push(hash[o[key]]);
}
fn(o, hash[o[key]]);
return r;
}, []);
}
var array = [{ name: "filter_color", value: "black" }, { name: "filter_color", value: "blue" }, { name: "filter_size", value: "l" }, { name: "filter_size", value: "m" }],
result = groupBy(array, 'name', function (s, t) {
t.value = t.value ? t.value + ',' + s.value : s.value;
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
#2
0
You can use Array.reduce
method to convert your existing array
into a new structure.
您可以使用Array.reduce方法将现有数组转换为新结构。
let arr = [
{"name":"filter_color","value":"black"},
{"name":"filter_color","value":"blue"},
{"name":"filter_size","value":"l"},
{"name":"filter_size","value":"m"}
];
let newArr = arr.reduce((acc, curr) => {
if(acc.some(obj => obj.name === curr.name)) {
acc.forEach(obj => {
if(obj.name === curr.name) {
obj.value = obj.value + " " + curr.value;
}
});
} else {
acc.push(curr);
}
return acc;
}, []);
console.log(newArr);
#1
0
You could use a hash table and group it by the wanted key.
您可以使用哈希表并按所需键对其进行分组。
function groupBy(array, key, fn) {
var hash = Object.create(null);
return array.reduce(function (r, o) {
if (!hash[o[key]]) {
hash[o[key]] = {};
hash[o[key]][key] = o[key];
r.push(hash[o[key]]);
}
fn(o, hash[o[key]]);
return r;
}, []);
}
var array = [{ name: "filter_color", value: "black" }, { name: "filter_color", value: "blue" }, { name: "filter_size", value: "l" }, { name: "filter_size", value: "m" }],
result = groupBy(array, 'name', function (s, t) {
t.value = t.value ? t.value + ',' + s.value : s.value;
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
#2
0
You can use Array.reduce
method to convert your existing array
into a new structure.
您可以使用Array.reduce方法将现有数组转换为新结构。
let arr = [
{"name":"filter_color","value":"black"},
{"name":"filter_color","value":"blue"},
{"name":"filter_size","value":"l"},
{"name":"filter_size","value":"m"}
];
let newArr = arr.reduce((acc, curr) => {
if(acc.some(obj => obj.name === curr.name)) {
acc.forEach(obj => {
if(obj.name === curr.name) {
obj.value = obj.value + " " + curr.value;
}
});
} else {
acc.push(curr);
}
return acc;
}, []);
console.log(newArr);