简体   繁体   English

如何在 myArray 的 Javascript 中创建一个包含嵌套数组的数组?

[英]How can I make an array instaed of nested arrays in Javascript from myArray?

I need to split riskTypeNo from each object and then assign selectedValue to each value and then create an array with all the objects.我需要从每个对象中拆分 riskTypeNo ,然后将 selectedValue 分配给每个值,然后创建一个包含所有对象的数组。 With my code I am getting nested arrays使用我的代码,我得到了嵌套数组

    const myArray = [
  {
    "fieldID": 1681977,
    "riskTypeNo": "A1;M1",
    "selectedValue": "4"
  },
  {
    "fieldID": 1681984,
    "riskTypeNo": "C6;M1",
    "selectedValue": "1"
  },
  {
    "fieldID": 1682084,
    "riskTypeNo": "A13;C6;M1;D5",
    "selectedValue": "3"
  }
];


console.log(
    myArray.map(item1 => {
    const riskTypeNo =  item1.riskTypeNo.split(";").map(item2 => ({ "riskTypeNo": item2,  "selectedValue": item1.selectedValue }));
    return riskTypeNo;   
  })
);

Desired Result is:期望的结果是:

[ 
{ riskTypeNo: "A1", selectedValue: "4" }, { riskTypeNo: "M1", selectedValue: "4" }], [{ riskTypeNo: "C6", selectedValue: "1" }, { riskTypeNo: "M1", selectedValue: "1" }], [{ riskTypeNo: "A13", selectedValue: "3" }, { riskTypeNo: "C6", selectedValue: "3" }, { riskTypeNo: "M1", selectedValue: "3" }, { riskTypeNo: "D5", selectedValue: "3" } 
}]

You can return the split array from inside the map method.您可以从map方法内部返回拆分数组。 But that will get you an array of arrays.但这会给你一个数组数组。 To flatten those arrays just use the array method flat .要展平这些数组,只需使用数组方法flat

This should work:这应该有效:

const result = myArray.map(el => {
    const riskTypes = el.riskTypeNo.split(";").map(riskTypeNo => ({
        riskTypeNo,
        selectedValue: el.selectedValue,
    }))
    return riskTypes;                                           
}).flat();

Here's one way:这是一种方法:

 const myArray = [ { "fieldID": 1681977, "riskTypeNo": "A1;M1", "selectedValue": "4" }, { "fieldID": 1681984, "riskTypeNo": "C6;M1", "selectedValue": "1" }, { "fieldID": 1682084, "riskTypeNo": "A13;C6;M1;D5", "selectedValue": "3" } ]; const result = myArray.reduce((prev,cur) => { let obj = []; cur.riskTypeNo.split(';').forEach((n) => { obj.push({riskTypeNo: n, selectedValue: cur.selectedValue}); }); prev.push(...obj); return prev; }, []); console.log(result);

you can use this method instead :您可以改用此方法:

const newArray = [];
myArray.forEach((item1) => {
  item1.riskTypeNo.split(";").forEach((item2) => {
    newArray.push({ riskTypeNo: item2, selectedValue: item1.selectedValue });
  });
});

result will be in newArray .结果将在newArray中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Javascript中创建自定义对象数组的数组? - How can I make an array of arrays of custom objects in Javascript? 如何从初始数组中制作 2 个不同的改组 arrays? javascript - How can i make 2 different shuffled arrays from an initial array? javascript 如何通过从单独的数组中匹配的字符串对象值从 3 个嵌套的 json 数组中提取对象值,然后在 html/javascript 中显示它 - How can I extract object values from 3 nested json arrays by matched string object values from a separate array and then display it in html/javascript 如何从javascript对象制作数组数组? - How to make an array of arrays from a javascript object? 如何将 arrays 数组从字符串转换为 Javascript 中的数字? - How can i turn an array of arrays from strings to numbers in Javascript? 如何在 JavaScript 中解构数组数组? - How can I destructure an array of arrays in JavaScript? 在 JavaScript 中,我无法创建字符串的 arrays 的唯一值,并从另一个数组中排除确定的值 - In JavaScript, I can't make unique values of arrays of strings, and exclude determinated values from another array 如何合并两个数组并使它们成为 JavaScript 中的对象数组 - How can I merge two arrays and make them array of objects in JavaScript JavaScript:如何合并这两个不完整对象的 arrays 并制作完整对象的数组 - JavaScript: how can I merge these two arrays of incomplete objects and make an array of complete objects 如何在JavaScript中创建嵌套数组? - How can I create a nested array in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM