次のようなオブジェクトの配列の操作:
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}
];
name
キーの一致する値の配列を減らして、次のような目的の出力を達成しようとしています。
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}]}
]
この試みは配列を減らしますが、上書きせずにネストされた値をマージする方法がありません。
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);
何が欠けていますか?
あなたのコードは少し目標に近いです、ただ何かを調整する必要があります。
以下のデモのコメントをご覧ください:
いつ acc.find
は何も検出せず、1つの要素をプッシュします{name:d.name, data: [value]}
見つかった場合は、1つをプッシュ{value: ...}
データプロパティに。
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)
名前をキーとしてマップを作成し、_Object destructing
_およびObject.values()
を使用して目的の結果を得るだけです。
_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);
_
存在しないかどうかを確認し、新しいオブジェクトを結果セットにプッシュすることで、見つかったオブジェクトの使用を簡略化できます。
次に、value
およびcount
を使用してオブジェクトを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; }
あなたはこのようなことをする必要があります:
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);
最初にインデックスを見つけてから、その行にデータをプッシュする必要があります。あなたはそれを見つけているだけで、新しい値を配列にプッシュしています。
あなたは遠くないです。これは、それを実現するためにコードの2行を単純に変更するだけです。
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)
違いは次のとおりです。
- 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 }] });