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评论98阅读模式
英文:

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创建一个对象,并为该对象创建一个联系人数组。
我已经完成了不使用展开运算符的代码,但想知道如何使用展开运算符来完成这个任务。

  1. // 我的工作代码,不使用展开运算符
  2. let res = Object.values(
  3. arr.reduce((acc, { objectid, userid, associationid }) => {
  4. (acc[objectid] ??= { objectid, contacts: [] }).contacts
  5. .push({ isdelete: '1', contactid: userid, associationid });
  6. return acc;
  7. }, {})
  8. );
  9. console.log(res);
  10. // 尝试以下使用展开运算符的代码,但不起作用
  11. const res = arr.reduce((acc, { objectid, userid, associationid }) =>
  12. ({ ...acc, (acc[objectid] ??= { objectid, contacts: [] }).contacts.push({ isdelete: '1', contactid: userid, associationid }) }),
  13. {})
  14. 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 -->

  1. const arr = [
  2. {
  3. &quot;objectid&quot;: 1,
  4. &quot;userid&quot;: &quot;1&quot;,
  5. &quot;associationid&quot;: &quot;123&quot;
  6. },
  7. {
  8. &quot;objectid&quot;: 1,
  9. &quot;userid&quot;: &quot;2&quot;,
  10. &quot;associationid&quot;: &quot;456&quot;
  11. },
  12. {
  13. &quot;objectid&quot;: 2,
  14. &quot;userid&quot;: &quot;2&quot;,
  15. &quot;associationid&quot;: &quot;456&quot;
  16. }
  17. ];

//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);

  1. //Tried the following code using spread operator but not working
  2. const res = arr.reduce((acc, {objectid,userid,associationid}) =&gt;
  3. ({ ...acc, (acc[objectid] ??= {objectid, contacts : []}).contacts.push({isdelete: &#39;1&#39;, contactid: userid, associationid}) }),
  4. {})
  5. 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 -->

  1. const arr = [{
  2. &quot;objectid&quot;: 1,
  3. &quot;userid&quot;: &quot;1&quot;,
  4. &quot;associationid&quot;: &quot;123&quot;
  5. },
  6. {
  7. &quot;objectid&quot;: 1,
  8. &quot;userid&quot;: &quot;2&quot;,
  9. &quot;associationid&quot;: &quot;456&quot;
  10. },
  11. {
  12. &quot;objectid&quot;: 2,
  13. &quot;userid&quot;: &quot;2&quot;,
  14. &quot;associationid&quot;: &quot;456&quot;
  15. }
  16. ];
  17. const res = arr.reduce((acc, {
  18. objectid,
  19. userid,
  20. associationid
  21. }) =&gt; {
  22. if (!acc[objectid]) {
  23. acc[objectid] = {
  24. objectid,
  25. contacts: []
  26. };
  27. }
  28. return {
  29. ...acc,
  30. [objectid]: {
  31. ...acc[objectid],
  32. contacts: [
  33. ...acc[objectid].contacts,
  34. {
  35. isdelete: &#39;1&#39;,
  36. contactid: userid,
  37. associationid
  38. }
  39. ]
  40. }
  41. };
  42. }, {});
  43. console.log(Object.values(res));

<!-- end snippet -->

答案2

得分: 1

以下是您要翻译的内容:

  1. 您可以尝试这段代码,我希望它能回答您的问题
  2. // 开始代码片段
  3. const arr = [{
  4. "objectid": 1,
  5. "userid": "1",
  6. "associationid": "123"
  7. },
  8. {
  9. "objectid": 1,
  10. "userid": "2",
  11. "associationid": "456"
  12. },
  13. {
  14. "objectid": 2,
  15. "userid": "2",
  16. "associationid": "456"
  17. }
  18. ];
  19. const res = Object.values(
  20. arr.reduce((acc, {
  21. objectid,
  22. userid,
  23. associationid
  24. }) => {
  25. acc[objectid] = {
  26. ...acc[objectid],
  27. objectid,
  28. contacts: [
  29. ...(acc[objectid]?.contacts ?? []),
  30. {
  31. isdelete: '1',
  32. contactid: userid,
  33. associationid
  34. },
  35. ],
  36. };
  37. return acc;
  38. }, {})
  39. );
  40. console.log(res);
  41. // 结束代码片段
英文:

You can try this code I hope it answers your question

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const arr = [{
  2. &quot;objectid&quot;: 1,
  3. &quot;userid&quot;: &quot;1&quot;,
  4. &quot;associationid&quot;: &quot;123&quot;
  5. },
  6. {
  7. &quot;objectid&quot;: 1,
  8. &quot;userid&quot;: &quot;2&quot;,
  9. &quot;associationid&quot;: &quot;456&quot;
  10. },
  11. {
  12. &quot;objectid&quot;: 2,
  13. &quot;userid&quot;: &quot;2&quot;,
  14. &quot;associationid&quot;: &quot;456&quot;
  15. }
  16. ];
  17. const res = Object.values(
  18. arr.reduce((acc, {
  19. objectid,
  20. userid,
  21. associationid
  22. }) =&gt; {
  23. acc[objectid] = {
  24. ...acc[objectid],
  25. objectid,
  26. contacts: [
  27. ...(acc[objectid]?.contacts ?? []),
  28. {
  29. isdelete: &#39;1&#39;,
  30. contactid: userid,
  31. associationid
  32. },
  33. ],
  34. };
  35. return acc;
  36. }, {})
  37. );
  38. console.log(res);

<!-- end snippet -->

答案3

得分: -2

你可以使用逗号运算符在修改累加器后始终返回它。

  1. const arr=[{objectid:1,userid:"1",associationid:"123"},{objectid:1,userid:"2",associationid:"456"},{objectid:2,userid:"2",associationid:"456"}];
  2. let res = Object.values(
  3. arr.reduce((acc, {objectid, userid, associationid}) =>
  4. ((acc[objectid] ??= {objectid, contacts : []}).contacts
  5. .push({isdelete: '1', contactid: userid, associationid}), acc), {})
  6. );
  7. 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 -->

  1. 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;}];
  2. let res = Object.values(
  3. arr.reduce((acc, {objectid, userid, associationid}) =&gt;
  4. ((acc[objectid] ??= {objectid, contacts : []}).contacts
  5. .push({isdelete: &#39;1&#39;, contactid: userid, associationid}), acc), {})
  6. );
  7. 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:

确定