数组转换、操作和格式化

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

Array conversion manipulation and formatting

问题

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

英文:

I have the following array:

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

I need to convert this to the following object:

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

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

答案1

得分: 1

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

for...of 方法

const data = [ { "level": "level3", "category": "car" }, { "level": "level1", "category": "bike" }, { "level": "level2", "category": "car" }, { "level": "level5", "category": "bike" } ];
const newObj = {};
for (const item of data) {
  const {level,category} = item;
  if (newObj[category]) {
    newObj[category].push(level);
  } else {
    newObj[category] = [level];
  }
}
console.log(newObj);

reduce 方法

const data = [ { "level": "level3", "category": "car" }, { "level": "level1", "category": "bike" }, { "level": "level2", "category": "car" }, { "level": "level5", "category": "bike" } ];
const newObj = data.reduce((acc, item) => {
    const { level, category } = item;
    acc[category] = acc[category] || [];
    acc[category].push(level);
    return acc;
}, {});
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 -->

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; } ];
const newObj = {};
for (const item of data) {
  const {level,category} = item;
  if (newObj[category]) {
    newObj[category].push(level);
  } else {
    newObj[category] = [level];
  }
}
console.log(newObj);

<!-- end snippet -->

reduce approach

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

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

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; } ];
const newObj = data.reduce((acc, item) =&gt; {
    const { level, category } = item;
    acc[category] = acc[category] || [];
    acc[category].push(level);
    return acc;
}, {});
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函数:

const array = [{
    "level": "level3",
    "category": "car"
}, {
    "level": "level1",
    "category": "bike"
}, {
    "level": "level2",
    "category": "car"
}, {
    "level": "level5",
    "category": "bike"
}];

const result = array.reduce((a, {level, category}) => {
    (a[category] = a[category] || []).push(level);
    return a;
}, {});

console.log(result);
.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 -->

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;    }],
      result = array.reduce((a, {level, category}) =&gt; {
        (a[category] = a[category] || []).push(level);
        return a;
      }, {});

console.log(result);

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

.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:

确定