如何在JavaScript中从aaa数组中创建一个对象?

huangapple go评论67阅读模式
英文:

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 = [
  {
    &quot;objectid&quot;: 111,
    &quot;userid&quot;: &quot;1&quot;,
    &quot;associationid&quot;: &quot;123&quot;
  },
  {
    &quot;objectid&quot;: 111,
    &quot;userid&quot;: &quot;2&quot;,
    &quot;associationid&quot;: &quot;456&quot;
  }
];

//Need to create an object like the following:
//{&quot;objectid&quot;: &quot;111&quot;, &quot;contacts&quot; : [{&quot;isdelete&quot;: &quot;1&quot;, &quot;contactid&quot;: &quot;1&quot;, &quot;associationid&quot;:&quot;123&quot;},{&quot;isdelete&quot;: &quot;1&quot;, &quot;contactid&quot;: &quot;2&quot;, &quot;associationid&quot;:&quot;456&quot;}]}

//I tried the following but it doesn&#39;t work:
 aaa.reduce((accumulator, {objectid,userid,associationid}) =&gt;
  ({ ...accumulator, &quot;objectid&quot;: objectid, &quot;contacts&quot;: (accumulator[&quot;contacts&quot;] || []).push(({&quot;isdelete&quot;: &quot;1&quot;, &quot;contactid&quot;: 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 = [
  {
    &quot;objectid&quot;: 111,
    &quot;userid&quot;: &quot;1&quot;,
    &quot;associationid&quot;: &quot;123&quot;
  },
  {
    &quot;objectid&quot;: 111,
    &quot;userid&quot;: &quot;2&quot;,
    &quot;associationid&quot;: &quot;456&quot;
  }
];
let res = Object.values(
  arr.reduce((acc, {objectid, userid, associationid}) =&gt; {
    (acc[objectid] ??= {objectid, contacts : []}).contacts
      .push({isdelete: &#39;1&#39;, 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 = [
  {
    &quot;objectid&quot;: 111,
    &quot;userid&quot;: &quot;1&quot;,
    &quot;associationid&quot;: &quot;123&quot;
  },
  {
    &quot;objectid&quot;: 111,
    &quot;userid&quot;: &quot;2&quot;,
    &quot;associationid&quot;: &quot;456&quot;
  }
];

//Need to create an object like the following:
//{&quot;objectid&quot;: &quot;111&quot;, &quot;contacts&quot; : [{&quot;isdelete&quot;: &quot;1&quot;, &quot;contactid&quot;: &quot;1&quot;, &quot;associationid&quot;:&quot;123&quot;},{&quot;isdelete&quot;: &quot;1&quot;, &quot;contactid&quot;: &quot;2&quot;, &quot;associationid&quot;:&quot;456&quot;}]}

//I tried the following but it doesn&#39;t work:
const ans = aaa.reduce((accumulator, {objectid,userid,associationid}) =&gt;
  ({ ...accumulator, &quot;objectid&quot;: objectid, &quot;contacts&quot;: (accumulator[&quot;contacts&quot;] || []).concat([{&quot;isdelete&quot;: &quot;1&quot;, &quot;contactid&quot;: userid, associationid}]) }),
  {})
  
  console.log({ans});

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月24日 06:03:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定