数组转换、操作和格式化

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

Array conversion manipulation and formatting

问题

{
car: ["level3", "level2"],
bike: ["level1", "level5"]
}

英文:

I have the following array:

  1. [
  2. {
  3. "level": "level3",
  4. "category": "car"
  5. },
  6. {
  7. "level": "level1",
  8. "category": "bike"
  9. },
  10. {
  11. "level": "level2",
  12. "category": "car"
  13. },
  14. {
  15. "level": "level5",
  16. "category": "bike"
  17. }
  18. ]

I need to convert this to the following object:

  1. {
  2. car: ["level3", "level2"],
  3. bike: ["level1", "level5"],
  4. }

What would be the best way to achieve this? Thanks in advance

答案1

得分: 1

你可以通过多种方法实现你想要的效果:

for...of 方法

  1. const data = [ { "level": "level3", "category": "car" }, { "level": "level1", "category": "bike" }, { "level": "level2", "category": "car" }, { "level": "level5", "category": "bike" } ];
  2. const newObj = {};
  3. for (const item of data) {
  4. const {level,category} = item;
  5. if (newObj[category]) {
  6. newObj[category].push(level);
  7. } else {
  8. newObj[category] = [level];
  9. }
  10. }
  11. console.log(newObj);

reduce 方法

  1. const data = [ { "level": "level3", "category": "car" }, { "level": "level1", "category": "bike" }, { "level": "level2", "category": "car" }, { "level": "level5", "category": "bike" } ];
  2. const newObj = data.reduce((acc, item) => {
  3. const { level, category } = item;
  4. acc[category] = acc[category] || [];
  5. acc[category].push(level);
  6. return acc;
  7. }, {});
  8. console.log(newObj);

第一种方法简单易懂,第二种方法更具功能性。但这两种方法的时间复杂度都是O(n),选择其中一种只是个人偏好和编码风格的问题。

英文:

You could achieve what you want with many approaches:

for...of approach

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

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

  1. const data = [ { &quot;level&quot;: &quot;level3&quot;, &quot;category&quot;: &quot;car&quot; }, { &quot;level&quot;: &quot;level1&quot;, &quot;category&quot;: &quot;bike&quot; }, { &quot;level&quot;: &quot;level2&quot;, &quot;category&quot;: &quot;car&quot; }, { &quot;level&quot;: &quot;level5&quot;, &quot;category&quot;: &quot;bike&quot; } ];
  2. const newObj = {};
  3. for (const item of data) {
  4. const {level,category} = item;
  5. if (newObj[category]) {
  6. newObj[category].push(level);
  7. } else {
  8. newObj[category] = [level];
  9. }
  10. }
  11. console.log(newObj);

<!-- end snippet -->

reduce approach

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

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

  1. const data = [ { &quot;level&quot;: &quot;level3&quot;, &quot;category&quot;: &quot;car&quot; }, { &quot;level&quot;: &quot;level1&quot;, &quot;category&quot;: &quot;bike&quot; }, { &quot;level&quot;: &quot;level2&quot;, &quot;category&quot;: &quot;car&quot; }, { &quot;level&quot;: &quot;level5&quot;, &quot;category&quot;: &quot;bike&quot; } ];
  2. const newObj = data.reduce((acc, item) =&gt; {
  3. const { level, category } = item;
  4. acc[category] = acc[category] || [];
  5. acc[category].push(level);
  6. return acc;
  7. }, {});
  8. console.log(newObj);

<!-- end snippet -->

The first approach is simple and understandable and the second is a more functional approach. But both approaches have a linear time complexity of O(n), choosing one of them is just a matter of personal preference and coding style.

答案2

得分: 1

使用Array.prototype.reduce函数:

  1. const array = [{
  2. "level": "level3",
  3. "category": "car"
  4. }, {
  5. "level": "level1",
  6. "category": "bike"
  7. }, {
  8. "level": "level2",
  9. "category": "car"
  10. }, {
  11. "level": "level5",
  12. "category": "bike"
  13. }];
  14. const result = array.reduce((a, {level, category}) => {
  15. (a[category] = a[category] || []).push(level);
  16. return a;
  17. }, {});
  18. console.log(result);
  1. .as-console-wrapper { max-height: 100% !important; top: 0; }
英文:

By using the function Array.prototype.reduce

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

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

  1. const array = [{ &quot;level&quot;: &quot;level3&quot;, &quot;category&quot;: &quot;car&quot; }, { &quot;level&quot;: &quot;level1&quot;, &quot;category&quot;: &quot;bike&quot; }, { &quot;level&quot;: &quot;level2&quot;, &quot;category&quot;: &quot;car&quot; }, { &quot;level&quot;: &quot;level5&quot;, &quot;category&quot;: &quot;bike&quot; }],
  2. result = array.reduce((a, {level, category}) =&gt; {
  3. (a[category] = a[category] || []).push(level);
  4. return a;
  5. }, {});
  6. console.log(result);

<!-- language: lang-css -->

  1. .as-console-wrapper { max-height: 100% !important; top: 0; }

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月22日 04:44:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527005.html
匿名

发表评论

匿名网友

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

确定