Destructure an array of objects and create a new array with array keys as a new value in JavaScript

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

Destructure an array of objects and create a new array with array keys as a new value in JavaScript

问题

以下是您要翻译的内容:

  1. 分解和数组对象并在JavaScript中从数组键创建新的对象数组
  2. 现有的对象数组如下所示 -
  3. ```javascript
  4. [
  5. {
  6. "Semester One" : [
  7. {
  8. id:"1",
  9. name:"Name1",
  10. },
  11. {
  12. id:"2",
  13. name:"Name2",
  14. }
  15. ],
  16. "Semester Two" : [
  17. {
  18. id:"3",
  19. name:"Name3",
  20. },
  21. {
  22. id:"4",
  23. name:"Name4",
  24. }
  25. ]
  26. }
  27. ]

我想要实现的是这样的:

  1. [
  2. {
  3. id:"1",
  4. name:"Name1",
  5. semester:"Semester One" // 数组键
  6. },
  7. {
  8. id:"2",
  9. name:"Name2",
  10. semester:"Semester One" // 数组键
  11. },
  12. {
  13. id:"3",
  14. name:"Name3",
  15. semester:"Semester Two" // 数组键
  16. },
  17. {
  18. id:"4",
  19. name:"Name4",
  20. semester:"Semester Two" // 数组键
  21. },
  22. ]

我能够成功创建具有值的对象数组,但我无法访问像"Semester One, Semester Two"这样的数组键。

到目前为止,我所做的是 -

  1. const arrObj = [];
  2. paperList.forEach((element) => {
  3. for (const key in element) {
  4. element[key].forEach((e, i) => {
  5. arrObj.push({
  6. ...e,
  7. semester: "", // 不知道如何获取这个值
  8. isChecked: "",
  9. });
  10. });
  11. }
  12. });
  13. console.log(arrObj);
  1. <details>
  2. <summary>英文:</summary>
  3. Distructure and array of object and create a new array of object with a new value from the array keys in Javascript.
  4. Existing Array of object is this -

[
{
"Semester One" : [
{
id:"1",
name:"Name1",
},
{
id:"2",
name:"Name2",
}
],
"Semester Two" : [
{
id:"3",
name:"Name3",
},
{
id:"4",
name:"Name4",
}
]
}
]

  1. ### what I want to achive, is this :

[
{
id:"1",
name:"Name1",
semester:"Semester One" // array key
},
{
id:"2",
name:"Name2",
semester:"Semester One" // array key
},
{
id:"3",
name:"Name3",
semester:"Semester Two" // array key
},
{
id:"4",
name:"Name4",
semester:"Semester Two" // array key
},
]

  1. I am able to successfully create an array of objects with a value, but I can&#39;t access the array keys like &quot;Semester One, Semester Two&quot;
  2. Here is what I have done so far -

const arrObj = [];
paperList.forEach((element) => {
for (const key in element) {
element[key].forEach((e, i) => {
arrObj.push({
...e,
semester: "", // don't know how to get this value
isChecked: "",
});
});
}
});
console.log(arrObj);

  1. </details>
  2. # 答案1
  3. **得分**: 0
  4. 你可以像这样实现数组,使用 foreach 并将其推送到新数组中。
  5. ```js
  6. var newArray = [];
  7. originalArray.forEach((item) => {
  8. Object.keys(item).forEach((key) => {
  9. item[key].forEach((obj) => {
  10. newArray.push({
  11. id: obj.id,
  12. name: obj.name,
  13. semester: key,
  14. });
  15. });
  16. });
  17. });
英文:

you can achieve the array like this, use a foreach and push to new array.

  1. var newArray = [];
  2. originalArray.forEach((item) =&gt; {
  3. Object.keys(item).forEach((key) =&gt; {
  4. item[key].forEach((obj) =&gt; {
  5. newArray.push({
  6. id: obj.id,
  7. name: obj.name,
  8. semester: key,
  9. });
  10. });
  11. });
  12. });

答案2

得分: 0

Here is the translated content without the code:

另一个可以使用的解决方案(较短):

  1. const arr = [
  2. {
  3. "第一学期": [
  4. {
  5. id: "1",
  6. name: "姓名1",
  7. },
  8. {
  9. id: "2",
  10. name: "姓名2",
  11. }
  12. ],
  13. "第二学期": [
  14. {
  15. id: "3",
  16. name: "姓名3",
  17. },
  18. {
  19. id: "4",
  20. name: "姓名4",
  21. }
  22. ]
  23. }
  24. ];
  25. const result = arr.flatMap(sem => // 遍历每个元素
  26. Object.keys(sem).flatMap(semId => // 获取学期标识符
  27. sem[semId].map(y => // 在学期上映射
  28. ({id: y.id, name: y.name, semester: semId}) // 格式化输出
  29. )
  30. )
  31. );
  32. console.log(result);

请注意,这是您提供的代码的中文翻译部分。如果您需要进一步的帮助或有其他问题,请随时提出。

英文:

Another solution you can use (shorter) :

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

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

  1. const arr = [
  2. {
  3. &quot;Semester One&quot; : [
  4. {
  5. id:&quot;1&quot;,
  6. name:&quot;Name1&quot;,
  7. },
  8. {
  9. id:&quot;2&quot;,
  10. name:&quot;Name2&quot;,
  11. }
  12. ],
  13. &quot;Semester Two&quot; : [
  14. {
  15. id:&quot;3&quot;,
  16. name:&quot;Name3&quot;,
  17. },
  18. {
  19. id:&quot;4&quot;,
  20. name:&quot;Name4&quot;,
  21. }
  22. ]
  23. }
  24. ];
  25. const result = arr.flatMap(sem =&gt; // iterate through each elements
  26. Object.keys(sem).flatMap(semId =&gt; // get semesters identifier
  27. sem[semId].map(y =&gt; // map on semesters
  28. ({id: y.id, name: y.name, semester: semId}) // format the output
  29. )
  30. )
  31. );
  32. console.log(result);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月24日 20:02:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323311.html
匿名

发表评论

匿名网友

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

确定