简体   繁体   English

如何将 arrays 的数组转换为嵌套的对象数组

[英]How to transform array of arrays into nested array of objects

I have an array of arrays:我有一个 arrays 数组:

const data = [
  ["10:00", "12:00", "13:00", "14:30"],
  ["10:00", "12:00", "13:00", "14:30"],
  ["10:00", "12:00", "13:00", "14:30"],
  ["10:00", "12:00", "13:00", "14:30"],
  ["10:00", "12:00", "13:00", "14:30"],
  ["10:00", "12:00", "13:00", "14:30"],
  ["10:00", "12:00", "13:00", "14:30"]
];

Here is the desired output :这是所需的output

const output = [
  [
    {
      startTime: "10:00",
      endTime: "12:00"
    },
    {
      startTime: "12:00",
      endTime: "13:00"
    },
    {
      startTime: "13:00",
      endTime: "14:30"
    }
  ],
  [
    {
      startTime: "10:00",
      endTime: "12:00"
    },
    {
      startTime: "12:00",
      endTime: "13:00"
    },
    {
      startTime: "13:00",
      endTime: "14:30"
    }
  ],
  [
    {
      startTime: "10:00",
      endTime: "12:00"
    },
    {
      startTime: "12:00",
      endTime: "13:00"
    },
    {
      startTime: "13:00",
      endTime: "14:30"
    }
  ],
  [
    {
      startTime: "10:00",
      endTime: "12:00"
    },
    {
      startTime: "12:00",
      endTime: "13:00"
    },
    {
      startTime: "13:00",
      endTime: "14:30"
    }
  ],
  [
    {
      startTime: "10:00",
      endTime: "12:00"
    },
    {
      startTime: "12:00",
      endTime: "13:00"
    },
    {
      startTime: "13:00",
      endTime: "14:30"
    }
  ],
  [
    {
      startTime: "10:00",
      endTime: "12:00"
    },
    {
      startTime: "12:00",
      endTime: "13:00"
    },
    {
      startTime: "13:00",
      endTime: "14:30"
    }
  ],
  [
    {
      startTime: "10:00",
      endTime: "12:00"
    },
    {
      startTime: "12:00",
      endTime: "13:00"
    },
    {
      startTime: "13:00",
      endTime: "14:30"
    }
  ]
];

Currently, I cannot get how to implement the nesting structure.目前,我无法了解如何实现嵌套结构。 Here is what I have tried so far to get the desired output :到目前为止,这是我为获得所需的output的尝试:

const transformData = () => {
  return data.map((item, index) => {
    return {
      startTime: item[index],
      endTime: item[index + 1]
    }
  });
};

You are missing the fact that you should not override your existing array data with your desired result, as you will need to iterate over it from beginning to end, including iterating through each sub array.您错过了一个事实,即您不应该用所需的结果覆盖现有数组data ,因为您需要从头到尾对其进行迭代,包括遍历每个子数组。 The desired output needs to be a new array instead.所需的output需要改为阵列。

Pass into your transformData the data object and have the function create a data structure with your desired output instead.data object 传递到您的transformData中,并让 function 使用您想要的output创建一个数据结构。 I also modified the inner logic of getting the startDate and endDate via modulus math... like so:我还修改了通过模数数学获取startDateendDate的内部逻辑......就像这样:

 const data = [ ["01:00", "02:00", "03:00", "04:00"], ["01:30", "02:30", "03:30", "04:30"], ["02:00", "03:00", "04:00", "05:00"], ["02:00", "03:30", "04:30", "05:30"], ["10:00", "10:01", "10:02", "10:03"], ["11:00", "12:00", "13:00", "14:00"], ["21:00", "21:30", "23:00", "23:30"] ]; let transform = (someData) => { let output = []; someData && someData.forEach(arr => { output.push(arr.map((elem, index) => { return { [index % 2 === 0? 'startTime': 'endTime']: elem }; })); }); return output; }; // Using HTML element to display results document.getElementById('result').innerHTML = JSON.stringify(transform(data));
 <div id="result"></div>

This should work:这应该有效:

const transformData = (data) => {
  return data.map((items) => {
    const aux = [];
    for (let i = 0; i < items.length - 1; i++) {
      aux.push({
        startTime: items[i],
        endTime: items[i + 1]
      });
    }
    return aux;
  });
};

You can try the following:您可以尝试以下方法:

data.map((arr) => {
    let x = [];
    for (let i = 0; i < arr.length - 1; i++) {
        x.push({ startTime: arr[i], endTime: arr[i + 1] });
    }
    return x;
});

You can try this way你可以试试这个方法

const timeList = [
    ["10:00", "12:00", "13:00", "14:30"],
    ["10:00", "12:00", "13:00", "14:30"],
    ["10:00", "12:00", "13:00", "14:30"],
    ["10:00", "12:00", "13:00", "14:30"],
    ["10:00", "12:00", "13:00", "14:30"],
    ["10:00", "12:00", "13:00", "14:30"],
    ["10:00", "12:00", "13:00", "14:30"]
];

const getTimeTable = (timeData: string[]) => {
    return timeData.slice(0, 3).map((data, index: number) => ({ startDate: data, endDate: timeData[index + 1] }));
}
const value = timeList.map((x) => getTimeTable(x));

console.log('value', ...value);

You could create a function to create your new data structure for the inner array and then map over that.您可以创建一个 function 来为内部数组创建新的数据结构,然后在上面创建 map。 In the following example compile_times uses reduce to create the new inner array.在以下示例中,compile_times 使用 reduce 来创建新的内部数组。 The fourth argument of reduce is the entire array, and here { [i + 1]: endTime } is used to pick the next item from the array and assign it to endTime. reduce 的第四个参数是整个数组,这里{ [i + 1]: endTime }用于从数组中挑选下一项并将其分配给 endTime。

 const data = [ ["10:00", "12:00", "13:00", "14:30"], ["10:00", "12:00", "13:00", "14:30"], ]; const compile_times = (times) => times.reduce( (acc, startTime, i, { [i + 1]: endTime }) => endTime? acc.concat({ startTime, endTime }): acc, [] ); const res = data.map(compile_times); console.log(res);

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

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