问题
Working with an array of objects like:
const arr = [
{name: "qewregf dqewafs", value: "qewregf dqewafs answer", count: 2},
{name: "survey with select", value: "survey with select answer", count: 2},
{name: "werasd", value: "Donald", count: 1},
{name: "werasd", value: "Jim", count: 1}
];
I am trying to reduce the array on matching values for the name
key and achieve an desired output like:
desiredOutput = [
{name: "qewregf dqewafs", data: [{value: "qewregf dqewafs answer", count: 2}]},
{name: "survey with select", data: [{value: "survey with select answer", count: 2}]},
{name: "werasd", data: [{value: "Donald", count: 1}, {value: "Jim", count: 1}]}
]
This attempt reduces the array, but I am missing how to merge the nested values without overwriting.
const arr = [{"name":"qewregf dqewafs","value":"qewregf dqewafs answer","count":2},{"name":"survey with select","value":"survey with select answer","count":2},{"name":"werasd","value":"Donald","count":1},{"name":"werasd","value":"Jim","count":1}];
const result = arr.reduce((acc, d) => {
const found = acc.find(a => a.name === d.name);
const value = { name: d.name, val: d.value };
if (found) {
acc.push(...value);
}
else {
acc.push({ name: d.name, data: [{ value: d.value }, { count: d.count }] });
}
return acc;
}, []);
console.log(result);
What am I missing?
回答1:
You codes are a little close to the goal, just need to adjust something.
Please see the comment in below demo:
When
acc.find
doesn't find anything, then push one element{name:d.name, data: [value]}
if found, then push one
{value: ...}
into data property.
const arr = [
{name: "qewregf dqewafs", value: "qewregf dqewafs answer", count: 2},
{name: "survey with select", value: "survey with select answer", count: 2},
{name: "werasd", value: "Donald", count: 1},
{name: "werasd", value: "Jim", count: 1}
];
const result = arr.reduce((acc, d) => {
const found = acc.find(a => a.name === d.name);
//const value = { name: d.name, val: d.value };
const value = { value: d.value, count: d.count }; // the element in data property
if (!found) {
//acc.push(...value);
acc.push({name:d.name, data: [value]}) // not found, so need to add data property
}
else {
//acc.push({ name: d.name, data: [{ value: d.value }, { count: d.count }] });
found.data.push(value) // if found, that means data property exists, so just push new element to found.data.
}
return acc;
}, []);
console.log(result)
回答2:
You could simplify the use of the found object by checking if it does not exist and push a new object to the result set.
Then push an object with value
and count
to data
.
const
array = [{ name: "qewregf dqewafs", value: "qewregf dqewafs answer", count: 2 }, { name: "survey with select", value: "survey with select answer", count: 2 }, { name: "werasd", value: "Donald", count: 1 }, { name: "werasd", value: "Jim", count: 1 }],
result = array.reduce((r, { name, value, count }) => {
var temp = r.find(o => name === o.name);
if (!temp) {
r.push(temp = { name, data: [] });
}
temp.data.push({ value, count });
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
回答3:
You need to do something like this:
const arr = [
{name: "qewregf dqewafs", value: "qewregf dqewafs answer", count: 2},
{name: "survey with select", value: "survey with select answer", count: 2},
{name: "werasd", value: "Donald", count: 1},
{name: "werasd", value: "Jim", count: 1}
];
const result = arr.reduce((acc, d) => {
let idx = acc.findIndex(a => a.name === d.name);
let val = { value: d.value, count: d.count };
if (idx > -1) {
acc[idx].data.push(val);
} else {
acc.push({ name: d.name, data: [val] });
}
return acc;
}, []);
console.log(result);
You have to find the index first, and then push your data into that row. You're just finding it but then pushing a new value to the array.
回答4:
You're not far off. This would be a simple change to two lines of your code to achieve it:
const arr = [
{name: "qewregf dqewafs", value: "qewregf dqewafs answer", count: 2},
{name: "survey with select", value: "survey with select answer", count: 2},
{name: "werasd", value: "Donald", count: 1},
{name: "werasd", value: "Jim", count: 1}
];
const result = arr.reduce((acc, d) => {
const found = acc.find(a => a.name === d.name);
const value = { name: d.name, val: d.value };
if (found) {
found.data.push(value);
}
else {
acc.push({ name: d.name, data: [{ value: d.value, count: d.count }] });
}
return acc;
}, []);
console.log(result)
Here are the differences:
- acc.push(...value);
+ found.data.push(value);
- acc.push({ name: d.name, data: [{ value: d.value }, { count: d.count }] });
+ acc.push({ name: d.name, data: [{ value: d.value, count: d.count }] });
回答5:
Simply create a map with name as key and use Object destructing
and Object.values()
to get the desired result.
const arr = [
{name: "qewregf dqewafs", value: "qewregf dqewafs answer", count: 2},
{name: "survey with select", value: "survey with select answer", count: 2},
{name: "werasd", value: "Donald", count: 1},
{name: "werasd", value: "Jim", count: 1}
];
let result = Object.values(arr.reduce((a,{name, ...props})=>{
if(!a[name])
a[name] = Object.assign({}, {name,data : [props]});
else
a[name].data.push(props);
return a;
},{}));
console.log(result);
来源:https://stackoverflow.com/questions/51935559/javascript-reduce-array-of-objects-by-key