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

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

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

//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}) =&gt;
  ({ ...acc, (acc[objectid] ??= {objectid, contacts : []}).contacts.push({isdelete: &#39;1&#39;, 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 = [{
    &quot;objectid&quot;: 1,
    &quot;userid&quot;: &quot;1&quot;,
    &quot;associationid&quot;: &quot;123&quot;
  },
  {
    &quot;objectid&quot;: 1,
    &quot;userid&quot;: &quot;2&quot;,
    &quot;associationid&quot;: &quot;456&quot;
  },
  {
    &quot;objectid&quot;: 2,
    &quot;userid&quot;: &quot;2&quot;,
    &quot;associationid&quot;: &quot;456&quot;
  }
];

const res = arr.reduce((acc, {
  objectid,
  userid,
  associationid
}) =&gt; {
  if (!acc[objectid]) {
    acc[objectid] = {
      objectid,
      contacts: []
    };
  }

  return {
    ...acc,
    [objectid]: {
      ...acc[objectid],
      contacts: [
        ...acc[objectid].contacts,
        {
          isdelete: &#39;1&#39;,
          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 = [{
    &quot;objectid&quot;: 1,
    &quot;userid&quot;: &quot;1&quot;,
    &quot;associationid&quot;: &quot;123&quot;
  },
  {
    &quot;objectid&quot;: 1,
    &quot;userid&quot;: &quot;2&quot;,
    &quot;associationid&quot;: &quot;456&quot;
  },
  {
    &quot;objectid&quot;: 2,
    &quot;userid&quot;: &quot;2&quot;,
    &quot;associationid&quot;: &quot;456&quot;
  }
];

const res = Object.values(
  arr.reduce((acc, {
    objectid,
    userid,
    associationid
  }) =&gt; {
    acc[objectid] = {
      ...acc[objectid],
      objectid,
      contacts: [
        ...(acc[objectid]?.contacts ?? []),
        {
          isdelete: &#39;1&#39;,
          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:&quot;1&quot;,associationid:&quot;123&quot;},{objectid:1,userid:&quot;2&quot;,associationid:&quot;456&quot;},{objectid:2,userid:&quot;2&quot;,associationid:&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}), acc), {})
);
console.log(res);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月15日 01:34:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248858.html
匿名

发表评论

匿名网友

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

确定