Let's say my array of objects looks like:
假设我的对象数组是这样的:
array1 = [
{
rule: {
column: "colName",
value: "val1"
}
},
{
rule: {
column: "colName",
value: "val2"
}
},
{
rule: {
column: "colName",
value: "val3"
}
},
{
rule: {
column: "colName2",
value: "val4"
}
}
]
I want to write something like a reduce
operation that checks if rule.column
are equal for all array elements and if so, to push into the values of one of them while removing the others. To give me something like:
我想写一些类似于reduce运算的东西来检查if规则。列对于所有数组元素都是相等的,如果是,则在删除其他元素的同时将其中一个元素推入其中一个元素的值。比如:
array2 = [
{
rule: {
column: "colName",
value: ["val1", "val2", "val3"]
}
},
{
rule: {
column: "colName2",
value: ["val4"]
}
}
]
Is there a straightforward way to do this? I can definitely write a regular function
to do this, but I am assuming I should be able to do a reduce
operation on array1
instead?
有直接的方法吗?我可以写一个普通的函数来做这个,但是我假设我应该可以在array1上做一个reduce运算?
Edit: the original question had otherProperty: {}
properties after rule
but my object doesn't actually have that (it's on the upper-level object)
编辑:最初的问题有otherProperty: {} properties after rule但我的对象实际上没有这个属性(它在上层对象上)
2 个解决方案
#1
3
Sure, just create an object with keys matching each column, push to the internal values
array, and then grab the object's values:
当然,只需创建一个对象,该对象的键与每个列匹配,将其推到内部值数组中,然后获取该对象的值:
const input = [{
rule: {
column: "colName",
value: 'v1'
},
otherProperty: 'otherProp',
},
{
rule: {
column: "colName",
value: 'v2'
},
otherProperty: 'otherProp',
},
{
rule: {
column: "colName",
value: 'val3',
},
otherProperty: 'otherProp',
},
{
rule: {
column: "colName2",
value: 'val4'
},
otherProperty: 'otherProp',
}
];
const outputCols = input.reduce((outputCols, { rule: { column, value }, ...otherProps}) => {
if (!outputCols[column]) {
outputCols[column] = { rule: { column, value: [] }, ...otherProps };
}
outputCols[column].rule.value.push(value);
return outputCols;
}, {});
console.log(Object.values(outputCols));
Note that if the otherProps
are different in subsequent objects, they'll be ignored currently.
注意,如果其他的道具在后续的对象中是不同的,它们将被忽略。
#2
1
You can use the function reduce
to group and then get the values as an array.
您可以使用reduce to group的函数,然后将值作为数组来获取。
The name otherProperty
is out of scope, but you can use either the function Object.assign
or the Spread syntax.
名称otherProperty不在范围之内,但是您可以使用function对象。分配或传播语法。
let array1 = [ { rule: { column: "colName", value: ["val1"] }, otherProperty: {} }, { rule: { column: "colName", value: ["val2"] }, otherProperty: {} }, { rule: { column: "colName", value: ["val3"] }, otherProperty: {} }, { rule: { column: "colName2", value: ["val4"] }, otherProperty: {} }],
result = Object.values(array1.reduce((a, c) => {
(a[c.rule.column] || (a[c.rule.column] = {rule: {column: c.rule.column, value: []}, otherProperty: {}})).rule.value.push(...c.rule.value);
return a;
}, {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
#1
3
Sure, just create an object with keys matching each column, push to the internal values
array, and then grab the object's values:
当然,只需创建一个对象,该对象的键与每个列匹配,将其推到内部值数组中,然后获取该对象的值:
const input = [{
rule: {
column: "colName",
value: 'v1'
},
otherProperty: 'otherProp',
},
{
rule: {
column: "colName",
value: 'v2'
},
otherProperty: 'otherProp',
},
{
rule: {
column: "colName",
value: 'val3',
},
otherProperty: 'otherProp',
},
{
rule: {
column: "colName2",
value: 'val4'
},
otherProperty: 'otherProp',
}
];
const outputCols = input.reduce((outputCols, { rule: { column, value }, ...otherProps}) => {
if (!outputCols[column]) {
outputCols[column] = { rule: { column, value: [] }, ...otherProps };
}
outputCols[column].rule.value.push(value);
return outputCols;
}, {});
console.log(Object.values(outputCols));
Note that if the otherProps
are different in subsequent objects, they'll be ignored currently.
注意,如果其他的道具在后续的对象中是不同的,它们将被忽略。
#2
1
You can use the function reduce
to group and then get the values as an array.
您可以使用reduce to group的函数,然后将值作为数组来获取。
The name otherProperty
is out of scope, but you can use either the function Object.assign
or the Spread syntax.
名称otherProperty不在范围之内,但是您可以使用function对象。分配或传播语法。
let array1 = [ { rule: { column: "colName", value: ["val1"] }, otherProperty: {} }, { rule: { column: "colName", value: ["val2"] }, otherProperty: {} }, { rule: { column: "colName", value: ["val3"] }, otherProperty: {} }, { rule: { column: "colName2", value: ["val4"] }, otherProperty: {} }],
result = Object.values(array1.reduce((a, c) => {
(a[c.rule.column] || (a[c.rule.column] = {rule: {column: c.rule.column, value: []}, otherProperty: {}})).rule.value.push(...c.rule.value);
return a;
}, {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }