JSON 数据重建的逻辑

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

Logic for reconstruction of json data

问题

希望你一切都好。

  1. [{
  2. "userId": 1,
  3. "id": 1,
  4. "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
  5. "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
  6. },
  7. {
  8. "userId": 2,
  9. "id": 2,
  10. "title": "qui est esse",
  11. "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
  12. },
  13. {
  14. "userId": 3,
  15. "id": 3,
  16. "title": "ea molestias quasi exercitationem repellat qui ipsa sit aut",
  17. "body": "et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis pariatur\nmolestiae porro eius odio et labore et velit aut"
  18. }]

考虑到每个 userId 下有多个对象,我想将它们按以下格式分组:

  1. const json2 = {
  2. 1: [{},{},{}],
  3. 2: [{},{},{}],
  4. 3: [{},{},{}]
  5. }

任何帮助都将不胜感激。提前感谢。

英文:

Hope you are doing well.

  1. [ {
  2. "userId": 1,
  3. "id": 1,
  4. "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
  5. "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
  6. },
  7. {
  8. "userId": 2,
  9. "id": 2,
  10. "title": "qui est esse",
  11. "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
  12. },
  13. {
  14. "userId": 3,
  15. "id": 3,
  16. "title": "ea molestias quasi exercitationem repellat qui ipsa sit aut",
  17. "body": "et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis pariatur\nmolestiae porro eius odio et labore et velit aut"
  18. },
  19. ]

Considering there are multiple objects for each userId and I want to group them together in the below format.

  1. const json2 = {
  2. 1: [{},{},{}],
  3. 2:[{},{},{}],
  4. 3:[{},{},{}],
  5. }

Any help will be appreciated.
Thanks in Advance

答案1

得分: 1

你可以使用reduce函数来实现,但下次在提问之前请提供你尝试过的内容。

  1. const input = [{
  2. "userId": 1,
  3. "id": 1,
  4. "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
  5. "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
  6. },
  7. {
  8. "userId": 2,
  9. "id": 2,
  10. "title": "qui est esse",
  11. "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
  12. },
  13. {
  14. "userId": 3,
  15. "id": 3,
  16. "title": "ea molestias quasi exercitationem repellat qui ipsa sit aut",
  17. "body": "et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis pariatur\nmolestiae porro eius odio et labore et velit aut"
  18. },
  19. ];
  20. const output = input.reduce((acc, el) => {
  21. acc[el.userId] ??=[];
  22. acc[el.userId].push(el);
  23. return acc;
  24. }, {})
  25. console.log(output);
英文:

You can do it using the reduce function, but next time please provide what you tried before asking.
<!-- begin snippet: js hide: false console: true babel: false -->

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

  1. const input = [{
  2. &quot;userId&quot;: 1,
  3. &quot;id&quot;: 1,
  4. &quot;title&quot;: &quot;sunt aut facere repellat provident occaecati excepturi optio reprehenderit&quot;,
  5. &quot;body&quot;: &quot;quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto&quot;
  6. },
  7. {
  8. &quot;userId&quot;: 2,
  9. &quot;id&quot;: 2,
  10. &quot;title&quot;: &quot;qui est esse&quot;,
  11. &quot;body&quot;: &quot;est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla&quot;
  12. },
  13. {
  14. &quot;userId&quot;: 3,
  15. &quot;id&quot;: 3,
  16. &quot;title&quot;: &quot;ea molestias quasi exercitationem repellat qui ipsa sit aut&quot;,
  17. &quot;body&quot;: &quot;et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis pariatur\nmolestiae porro eius odio et labore et velit aut&quot;
  18. },
  19. ];
  20. const output = input.reduce((acc, el) =&gt; {
  21. acc[el.userId]??=[];
  22. acc[el.userId].push(el);
  23. return acc;
  24. }, {})
  25. console.log(output);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月14日 19:47:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447396.html
匿名

发表评论

匿名网友

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

确定