[英]How to stop duplicating keys for values
I have an array serialNumbers. 我有一个数组serialNumbers。 It can look like this. 它看起来像这样。
lot: 1 Serial: 2
lot: 1 Serial: 3
lot: 1 Serial: 4
... and so on. ... 等等。 or it may look like 或看起来像
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
})
}
This is what I was trying to use to get things working but this creates a key and value for each one listed. 这就是我试图用来使事情正常运行的方法,但这为列出的每一个创建了键和值。 I want my outcome to be an object like: 我希望我的结果成为像这样的对象:
Key: 1 Value: 2, 3, 4, 5, 9
Key: 8 Value: 2, 4
Can anyone help me figure this out? 谁能帮我解决这个问题? Thank you! 谢谢!
Not sure if there is a better/easier way, but this would do it:- 不知道是否有更好/更简便的方法,但这可以做到:
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);
An alternative to accomplish your requirement, is using the function reduce
to group the keys and values. 要满足您的要求,另一种方法是使用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; }
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]}}
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;
}
}
The code above will add values to their corresponding keys. 上面的代码会将值添加到其相应的键中。
here's a simple approach using two forEach
loops : 这是使用两个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)
You can use a Map
for this. 您可以为此使用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(",")); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.