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

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

How can I create an object from aaa array in javascript?

问题

  1. //开始代码段
  2. const aaa = [
  3. {
  4. "objectid": 111,
  5. "userid": "1",
  6. "associationid": "123"
  7. },
  8. {
  9. "objectid": 111,
  10. "userid": "2",
  11. "associationid": "456"
  12. }
  13. ];
  14. //需要创建如下对象:
  15. //{
  16. //"objectid": "111",
  17. //"contacts": [
  18. // {"isdelete": "1", "contactid": "1", "associationid": "123"},
  19. // {"isdelete": "1", "contactid": "2", "associationid": "456"}
  20. //]}
  21. //我尝试了以下方法,但它不起作用:
  22. aaa.reduce((accumulator, {objectid,userid,associationid}) =>
  23. ({ ...accumulator, "objectid": objectid, "contacts": (accumulator["contacts"] || []).push({"isdelete": "1", "contactid": userid, "associationid": associationid}) }),
  24. {})
  25. //结束代码段
英文:

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

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

  1. const aaa = [
  2. {
  3. &quot;objectid&quot;: 111,
  4. &quot;userid&quot;: &quot;1&quot;,
  5. &quot;associationid&quot;: &quot;123&quot;
  6. },
  7. {
  8. &quot;objectid&quot;: 111,
  9. &quot;userid&quot;: &quot;2&quot;,
  10. &quot;associationid&quot;: &quot;456&quot;
  11. }
  12. ];
  13. //Need to create an object like the following:
  14. //{&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;}]}
  15. //I tried the following but it doesn&#39;t work:
  16. aaa.reduce((accumulator, {objectid,userid,associationid}) =&gt;
  17. ({ ...accumulator, &quot;objectid&quot;: objectid, &quot;contacts&quot;: (accumulator[&quot;contacts&quot;] || []).push(({&quot;isdelete&quot;: &quot;1&quot;, &quot;contactid&quot;: userid, associationid})) }),
  18. {})

<!-- end snippet -->

答案1

得分: 1

你可以使用Array#reduce方法,配合一个对象来存储每个id的数值。

  1. const arr = [
  2. {
  3. "objectid": 111,
  4. "userid": "1",
  5. "associationid": "123"
  6. },
  7. {
  8. "objectid": 111,
  9. "userid": "2",
  10. "associationid": "456"
  11. }
  12. ];
  13. let res = Object.values(
  14. arr.reduce((acc, {objectid, userid, associationid}) => {
  15. (acc[objectid] ??= {objectid, contacts : []}).contacts
  16. .push({isdelete: '1', contactid: userid, associationid});
  17. return acc;
  18. }, {})
  19. );
  20. console.log(res);
  21. // 使用 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 -->

  1. const arr = [
  2. {
  3. &quot;objectid&quot;: 111,
  4. &quot;userid&quot;: &quot;1&quot;,
  5. &quot;associationid&quot;: &quot;123&quot;
  6. },
  7. {
  8. &quot;objectid&quot;: 111,
  9. &quot;userid&quot;: &quot;2&quot;,
  10. &quot;associationid&quot;: &quot;456&quot;
  11. }
  12. ];
  13. let res = Object.values(
  14. arr.reduce((acc, {objectid, userid, associationid}) =&gt; {
  15. (acc[objectid] ??= {objectid, contacts : []}).contacts
  16. .push({isdelete: &#39;1&#39;, contactid: userid, associationid});
  17. return acc;
  18. }, {})
  19. );
  20. console.log(res);
  21. // use res[0] to get just the first object itself

<!-- end snippet -->

答案2

得分: 1

#push 返回数组的长度而不是新数组。如果你想要一个简短的代码,你可以使用 #concat,将要添加的项包装在另一个数组中。

从文档中可以看到:

push() 方法将一个或多个元素添加到数组的末尾,并返回数组的新长度。

concat() 方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。

  1. const aaa = [
  2. {
  3. "objectid": 111,
  4. "userid": "1",
  5. "associationid": "123"
  6. },
  7. {
  8. "objectid": 111,
  9. "userid": "2",
  10. "associationid": "456"
  11. }
  12. ];
  13. // 需要创建一个对象,格式如下:
  14. // {"objectid": "111", "contacts": [{"isdelete": "1", "contactid": "1", "associationid": "123"}, {"isdelete": "1", "contactid": "2", "associationid": "456"}]}
  15. // 我尝试了以下方法,但它不起作用:
  16. const ans = aaa.reduce((accumulator, {objectid, userid, associationid}) =>
  17. ({ ...accumulator, "objectid": objectid, "contacts": (accumulator["contacts"] || []).concat([{"isdelete": "1", "contactid": userid, associationid}]) }),
  18. {})
  19. 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 -->

  1. const aaa = [
  2. {
  3. &quot;objectid&quot;: 111,
  4. &quot;userid&quot;: &quot;1&quot;,
  5. &quot;associationid&quot;: &quot;123&quot;
  6. },
  7. {
  8. &quot;objectid&quot;: 111,
  9. &quot;userid&quot;: &quot;2&quot;,
  10. &quot;associationid&quot;: &quot;456&quot;
  11. }
  12. ];
  13. //Need to create an object like the following:
  14. //{&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;}]}
  15. //I tried the following but it doesn&#39;t work:
  16. const ans = aaa.reduce((accumulator, {objectid,userid,associationid}) =&gt;
  17. ({ ...accumulator, &quot;objectid&quot;: objectid, &quot;contacts&quot;: (accumulator[&quot;contacts&quot;] || []).concat([{&quot;isdelete&quot;: &quot;1&quot;, &quot;contactid&quot;: userid, associationid}]) }),
  18. {})
  19. 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:

确定