简体   繁体   English

如何使用 for 循环将 object 数组转换为数组列表

[英]How to covert object array to Array List using for loop

Hey I am trying to convert object in array list i tried a lot of but i could not convert here is the code that i tried嘿,我正在尝试在数组列表中转换 object 我尝试了很多但我无法转换这里是我尝试过的代码

const dat1 = [];常量 dat1 = [];

for (let i = 0; i < topology.length; i++) {
  const data = [data1[i]];

  dat1.push({
    value:data
  });
}

Here is the result that i got of value .这是我得到的value结果。

const topology = [
      {
        label: "secondary",
        value: 0.10558737933770979
      },
      {
        label: "unclassified",
        value: 0.07702637029193307
      },
      {
        label: "residential",
        value: 0.05898100977978933
      },
      {
        label: "tertiary",
        value: 0.3012573789201084
      },
      {
        label: "primary",
        value: 0.44342463442819086
      },
      {
        label: "primary_link",
        value: 0.013723227242268547
      },
    ];

and Here is the result that i want value look like in Array form.这是我希望value看起来像Array形式的结果。

const topology = [
      {
        label: "secondary",
        value: [0.10558737933770979]
      },
      {
        label: "unclassified",
        value: [0.07702637029193307]
      },
      {
        label: "residential",
        value: [0.05898100977978933]
      },
      {
        label: "tertiary",
        value: [0.3012573789201084]
      },
      {
        label: "primary",
        value: [0.44342463442819086]
      },
      {
        label: "primary_link",
        value: [0.01372322724226854]
      },
    ];

You could map the original topology and then just return the value wrapped in an array, like so:您可以 map 原始topology ,然后返回包装在数组中的value ,如下所示:

 const topology = [ { label: "secondary", value: 0.10558737933770979, }, { label: "unclassified", value: 0.07702637029193307, }, { label: "residential", value: 0.05898100977978933, }, { label: "tertiary", value: 0.3012573789201084, }, { label: "primary", value: 0.44342463442819086, }, { label: "primary_link", value: 0.013723227242268547, }, ]; let result = topology.map(({ label, value }) => ({ label, value: [value] })); console.log(result);

You can also follow your approach, like so:您也可以遵循您的方法,如下所示:

const data1 = [];
for (let i = 0; i < topology.length; i++) {
  const data = [topology[i].value];
  data1.push({
    label: topology[i].label,
    value: data,
  });
}

You were very close, just missing getting the value to be wrapped, ie you want the "old data", not the current new, so not [data1[i]] but [topology[i].value] and then adding the label to the new object in data1 array.您非常接近,只是缺少要包装的值,即您想要“旧数据”,而不是当前的新数据,所以不是[data1[i]]而是[topology[i].value]然后添加 label到data1数组中的新 object。

 const topology = [ { label: "secondary", value: 0.10558737933770979, }, { label: "unclassified", value: 0.07702637029193307, }, { label: "residential", value: 0.05898100977978933, }, { label: "tertiary", value: 0.3012573789201084, }, { label: "primary", value: 0.44342463442819086, }, { label: "primary_link", value: 0.013723227242268547, }, ]; const data1 = []; for (let i = 0; i < topology.length; i++) { const data = [topology[i].value]; data1.push({ label: topology[i].label, value: data, }); } console.log(data1);

You could do something like this:你可以这样做:

const changed = topology.map(({ label, value }) => ({
  label,
  value: [value]
}));

Basically just transforms the original data to wrap the value as an array with a single element.基本上只是转换原始数据以将值包装为具有单个元素的数组。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM