我有一个数组serialNumbers。它看起来像这样。
lot: 1 Serial: 2
lot: 1 Serial: 3
lot: 1 Serial: 4
......等等。或者看起来像
lot: 1 Serial: 5
lot: 1 Serial: 9
lot: 8 Serial: 2
lot: 8 Serial: 4
var dictSerials = []
if (serialNumbers.length > 0)
for (var i of serialNumbers) {
dictSerials.push({
key: i.value.lot,
value: i.value.serial
})
}
这就是我试图让事情发挥作用的原因,但这会为列出的每一个创建一个键和值。我希望我的结果成为像这样的对象:
Key: 1 Value: 2, 3, 4, 5, 9
Key: 8 Value: 2, 4
任何人都可以帮我解决这个问题吗?谢谢!
答案 0 :(得分:1)
不确定是否有更好/更简单的方法,但这样做: -
var array = [{
name: "foo1",
value: "val1"
}, {
name: "foo1",
value: ["val2", "val3"]
}, {
name: "foo2",
value: "val4"
}];
var output = [];
array.forEach(function(value) {
var existing = output.filter(function(v, i) {
return v.name == value.name;
});
if (existing.length) {
var existingIndex = output.indexOf(existing[0]);
output[existingIndex].value = output[existingIndex].value.concat(value.value);
} else {
if (typeof value.value == 'string')
value.value = [value.value];
output.push(value);
}
});
console.dir(output);
答案 1 :(得分:1)
完成您的要求的替代方法是使用函数reduce
对键和值进行分组。
let array = [{lot: 1, Serial: 2},{lot: 1, Serial: 3},{lot: 1, Serial: 4},{lot: 1, Serial: 5},{lot: 1,Serial: 9},{lot: 8,Serial: 2},{lot: 8,Serial: 4}],
result = Object.values(array.reduce((a, c) => {
(a[c.lot] || (a[c.lot] = {Key: c.lot, Value: []})).Value.push(c.Serial);
return a;
}, {}));
console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:0)
function getDictSerials(serialNumbers) {
var dictSerials = {};
if (serialNumbers.length > 0) {
for (let i of serialNumbers) {
let lot = i.lot;
let serial = i.serial;
if (!dictSerials[lot]) { // If the key isn't found - intiate an array
dictSerials[lot] = [];
}
dictSerials[lot].push(serial) // push the serial to the corresponding lot
}
}
return dictSerials;
}
let serialNumbers = [
{lot : 1, serial : 2},
{lot : 1, serial : 3},
{lot : 1, serial : 4},
{lot : 1, serial : 5},
{lot : 1, serial : 9},
{lot : 8, serial : 2},
{lot : 8, serial : 4}
];
let desiredObj = {
dictSerials: getDictSerials(serialNumbers)
}
JSON.stringify(desiredObj); // {"dictSerials":{"1":[2,3,4,5,9],"8":[2,4]}}
答案 3 :(得分:0)
var dictSerials = []
if (serialNumbers.length > 0) {
for (var i of serialNumbers) {
if(!dictSerials[i.value.lot]){
dictSerials[i.value.lot] = [];
}
dictSerials[i.value.lot][] = i.value.serial;
}
}
上面的代码会将值添加到相应的键中。
答案 4 :(得分:0)
这是一个使用两个forEach
循环的简单方法:
const arr = [
{lot : 1, Serial : 2},
{lot : 1, Serial : 3},
{lot : 1, Serial : 4},
{lot : 1, Serial : 5},
{lot : 1, Serial : 9},
{lot : 8, Serial : 2},
{lot : 8, Serial : 4}
]
let grouped = [],
result = [];
arr.forEach((e) => {
(result[e.lot] || (result[e.lot] = [])).push(e.Serial)
})
result.forEach((e, i) => {
grouped.push({
"key": i,
"value": e.join(',')
})
});
console.log(grouped)
答案 5 :(得分:0)
您可以使用Map
。
var serialNumbers = [
{value:{lot:1, serial: 2}},
{value:{lot:1, serial: 3}},
{value:{lot:1, serial: 4}},
{value:{lot:1, serial: 5}},
{value:{lot:1, serial: 9}},
{value:{lot:8, serial: 2}},
{value:{lot:8, serial: 4}},
];
var dictSerials = new Map();
if (serialNumbers.length > 0) {
for (var i of serialNumbers) {
if(!dictSerials.has(i.value.lot)) {
dictSerials.set(i.value.lot, [i.value.serial]);
} else {
var v = dictSerials.get(i.value.lot);
v.push(i.value.serial);
dictSerials.set(i.value.lot, v);
}
}
}
//Output
for (var [key, value] of dictSerials.entries()) {
console.log("Key: " + key + " Value: " + value.join(","));
}