英文:
How can I create an object from aaa array in javascript?
问题
//开始代码段
const aaa = [
{
"objectid": 111,
"userid": "1",
"associationid": "123"
},
{
"objectid": 111,
"userid": "2",
"associationid": "456"
}
];
//需要创建如下对象:
//{
//"objectid": "111",
//"contacts": [
// {"isdelete": "1", "contactid": "1", "associationid": "123"},
// {"isdelete": "1", "contactid": "2", "associationid": "456"}
//]}
//我尝试了以下方法,但它不起作用:
aaa.reduce((accumulator, {objectid,userid,associationid}) =>
({ ...accumulator, "objectid": objectid, "contacts": (accumulator["contacts"] || []).push({"isdelete": "1", "contactid": userid, "associationid": associationid}) }),
{})
//结束代码段
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const aaa = [
{
"objectid": 111,
"userid": "1",
"associationid": "123"
},
{
"objectid": 111,
"userid": "2",
"associationid": "456"
}
];
//Need to create an object like the following:
//{"objectid": "111", "contacts" : [{"isdelete": "1", "contactid": "1", "associationid":"123"},{"isdelete": "1", "contactid": "2", "associationid":"456"}]}
//I tried the following but it doesn't work:
aaa.reduce((accumulator, {objectid,userid,associationid}) =>
({ ...accumulator, "objectid": objectid, "contacts": (accumulator["contacts"] || []).push(({"isdelete": "1", "contactid": userid, associationid})) }),
{})
<!-- end snippet -->
答案1
得分: 1
你可以使用Array#reduce
方法,配合一个对象来存储每个id的数值。
const arr = [
{
"objectid": 111,
"userid": "1",
"associationid": "123"
},
{
"objectid": 111,
"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});
return acc;
}, {})
);
console.log(res);
// 使用 res[0] 来获取第一个对象本身
希望这个翻译对你有所帮助。
英文:
You can use Array#reduce
with an object to store the values for each id.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const arr = [
{
"objectid": 111,
"userid": "1",
"associationid": "123"
},
{
"objectid": 111,
"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});
return acc;
}, {})
);
console.log(res);
// use res[0] to get just the first object itself
<!-- end snippet -->
答案2
得分: 1
#push
返回数组的长度而不是新数组。如果你想要一个简短的代码,你可以使用 #concat
,将要添加的项包装在另一个数组中。
从文档中可以看到:
push() 方法将一个或多个元素添加到数组的末尾,并返回数组的新长度。
concat() 方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。
const aaa = [
{
"objectid": 111,
"userid": "1",
"associationid": "123"
},
{
"objectid": 111,
"userid": "2",
"associationid": "456"
}
];
// 需要创建一个对象,格式如下:
// {"objectid": "111", "contacts": [{"isdelete": "1", "contactid": "1", "associationid": "123"}, {"isdelete": "1", "contactid": "2", "associationid": "456"}]}
// 我尝试了以下方法,但它不起作用:
const ans = aaa.reduce((accumulator, {objectid, userid, associationid}) =>
({ ...accumulator, "objectid": objectid, "contacts": (accumulator["contacts"] || []).concat([{"isdelete": "1", "contactid": userid, associationid}]) }),
{})
console.log({ans});
希望这能帮助你解决问题。
英文:
#push
returns the array length and not the new array. You can use #concat
instead while wrapping the pushed item into another array, if you are looking for a short code.
Fromt the docs:
> The push() method adds one or more elements to the end of an array and returns the new length of the array.
> The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const aaa = [
{
"objectid": 111,
"userid": "1",
"associationid": "123"
},
{
"objectid": 111,
"userid": "2",
"associationid": "456"
}
];
//Need to create an object like the following:
//{"objectid": "111", "contacts" : [{"isdelete": "1", "contactid": "1", "associationid":"123"},{"isdelete": "1", "contactid": "2", "associationid":"456"}]}
//I tried the following but it doesn't work:
const ans = aaa.reduce((accumulator, {objectid,userid,associationid}) =>
({ ...accumulator, "objectid": objectid, "contacts": (accumulator["contacts"] || []).concat([{"isdelete": "1", "contactid": userid, associationid}]) }),
{})
console.log({ans});
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论