英文:
How can I create an Array of objects from arr array in javascript using spread operator? - Trying to fit all of the code on to one line
问题
需要为每个objectid创建一个对象,并为该对象创建一个联系人数组。
我已经完成了不使用展开运算符的代码,但想知道如何使用展开运算符来完成这个任务。
// 我的工作代码,不使用展开运算符
let res = Object.values(
arr.reduce((acc, { objectid, userid, associationid }) => {
(acc[objectid] ??= { objectid, contacts: [] }).contacts
.push({ isdelete: '1', contactid: userid, associationid });
return acc;
}, {})
);
console.log(res);
// 尝试以下使用展开运算符的代码,但不起作用
const res = arr.reduce((acc, { objectid, userid, associationid }) =>
({ ...acc, (acc[objectid] ??= { objectid, contacts: [] }).contacts.push({ isdelete: '1', contactid: userid, associationid }) }),
{})
console.log(res);
英文:
Need to create one object per objectid and create an array of contacts for that object.
I have done it without spread operator but wondering how this can be done using spread operator.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const arr = [
{
"objectid": 1,
"userid": "1",
"associationid": "123"
},
{
"objectid": 1,
"userid": "2",
"associationid": "456"
},
{
"objectid": 2,
"userid": "2",
"associationid": "456"
}
];
//My working code without using spread operator
let res = Object.values(
arr.reduce((acc, {objectid, userid, associationid}) => {
(acc[objectid] ??= {objectid, contacts : []}).contacts
.push({isdelete: '1', contactid: userid, associationid});
return acc;
}, {})
);
console.log(res);
//Tried the following code using spread operator but not working
const res = arr.reduce((acc, {objectid,userid,associationid}) =>
({ ...acc, (acc[objectid] ??= {objectid, contacts : []}).contacts.push({isdelete: '1', contactid: userid, associationid}) }),
{})
console.log(res);
<!-- end snippet -->
答案1
得分: 1
你试图在同时展开对象的情况下对对象中的数组进行变异,这在语法上是不正确的。我正在使用展开运算符来保留先前的联系人并添加新的联系人。
英文:
You are trying to mutate an array within an object while spreading the object at the same time, which is syntactically incorrect. I am using the spread operator to keep the previous contacts and add the new one.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const arr = [{
"objectid": 1,
"userid": "1",
"associationid": "123"
},
{
"objectid": 1,
"userid": "2",
"associationid": "456"
},
{
"objectid": 2,
"userid": "2",
"associationid": "456"
}
];
const res = arr.reduce((acc, {
objectid,
userid,
associationid
}) => {
if (!acc[objectid]) {
acc[objectid] = {
objectid,
contacts: []
};
}
return {
...acc,
[objectid]: {
...acc[objectid],
contacts: [
...acc[objectid].contacts,
{
isdelete: '1',
contactid: userid,
associationid
}
]
}
};
}, {});
console.log(Object.values(res));
<!-- end snippet -->
答案2
得分: 1
以下是您要翻译的内容:
您可以尝试这段代码,我希望它能回答您的问题
// 开始代码片段
const arr = [{
"objectid": 1,
"userid": "1",
"associationid": "123"
},
{
"objectid": 1,
"userid": "2",
"associationid": "456"
},
{
"objectid": 2,
"userid": "2",
"associationid": "456"
}
];
const res = Object.values(
arr.reduce((acc, {
objectid,
userid,
associationid
}) => {
acc[objectid] = {
...acc[objectid],
objectid,
contacts: [
...(acc[objectid]?.contacts ?? []),
{
isdelete: '1',
contactid: userid,
associationid
},
],
};
return acc;
}, {})
);
console.log(res);
// 结束代码片段
英文:
You can try this code I hope it answers your question
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const arr = [{
"objectid": 1,
"userid": "1",
"associationid": "123"
},
{
"objectid": 1,
"userid": "2",
"associationid": "456"
},
{
"objectid": 2,
"userid": "2",
"associationid": "456"
}
];
const res = Object.values(
arr.reduce((acc, {
objectid,
userid,
associationid
}) => {
acc[objectid] = {
...acc[objectid],
objectid,
contacts: [
...(acc[objectid]?.contacts ?? []),
{
isdelete: '1',
contactid: userid,
associationid
},
],
};
return acc;
}, {})
);
console.log(res);
<!-- end snippet -->
答案3
得分: -2
你可以使用逗号运算符在修改累加器后始终返回它。
const arr=[{objectid:1,userid:"1",associationid:"123"},{objectid:1,userid:"2",associationid:"456"},{objectid:2,userid:"2",associationid:"456"}];
let res = Object.values(
arr.reduce((acc, {objectid, userid, associationid}) =>
((acc[objectid] ??= {objectid, contacts : []}).contacts
.push({isdelete: '1', contactid: userid, associationid}), acc), {})
);
console.log(res);
英文:
You can use the comma operator to always return the accumulator after modifying it.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const arr=[{objectid:1,userid:"1",associationid:"123"},{objectid:1,userid:"2",associationid:"456"},{objectid:2,userid:"2",associationid:"456"}];
let res = Object.values(
arr.reduce((acc, {objectid, userid, associationid}) =>
((acc[objectid] ??= {objectid, contacts : []}).contacts
.push({isdelete: '1', contactid: userid, associationid}), acc), {})
);
console.log(res);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论