简体   繁体   English

JavaScript 将两个 arrays 合并为一个 object

[英]JavaScript Combine two arrays into one object

I have two arrays, slicesRank and slicesCount with following structure.我有两个 arrays, slicesRankslicesCount ,结构如下。 Each element has id and value, which is an array of 46. values is composed of date and measurement.每个元素都有 id 和 value,它是一个 46 个数组。 values 由 date 和 measure 组成。

eg sliceRank[0] : {id: Catan=rank, values(Array(46)): {date, measurement} }例如sliceRank[0] : {id: Catan=rank, values(Array(46)): {date, measurement} }

eg sliceCount[0] : {id: Catan=count, values(Array(46)): {date, measurement} }例如sliceCount[0] : {id: Catan=count, values(Array(46)): {date, measurement} }

What should I do if I want to combine the elements with the same prefix in id names.如果我想在 id 名称中组合具有相同前缀的元素,我应该怎么做。 For example, the first element in this two arrays.例如,这两个 arrays 中的第一个元素。

The desired sturcture would be {id: Catan, values(Array(46)): {date, count, rank} }所需的结构是{id: Catan, values(Array(46)): {date, count, rank} }

I tried the following, but the values shows undifined.我尝试了以下方法,但values显示未定义。

    for(i=0; i<slicesRank.length; i++) {
        var newElement = {};
        newElement['id'] = slicesRank[i].id.replace('=rank', '');
        newElement['values'] = {
          date: slicesRank[i].date,
          rank: slicesRank[i].measurement,
          count: slicesCount[i].measurement
        };
        sliceNew.push(newElement);
    };

The structure of slicesRank is like this: slicesRank的结构是这样的:

在此处输入图像描述 在此处输入图像描述

By slice I think you mean Array .切片我认为你的意思是Array

const combinedArray = sliceRank.map((sliceItem, itemIndex) => {
    return {
        id,
        values: sliceItem.values.map(({ date, measurement }, valueIndex) => ({
            date,
            count: sliceCount[itemIndex][valueIndex].measurement,
            rank: measurement,
        }))
    }
})

This solution returns an array of objects combined using sliceRank and sliceCount .此解决方案返回使用sliceRanksliceCount组合的对象数组。 So now each array item now contains values of structure { date, count, rank } .所以现在每个数组项现在都包含结构{ date, count, rank }values Array.map is immutable so both slices will be the same. Array.map是不可变的,因此两个切片将相同。

Here's a sample transformation that you can try:这是您可以尝试的示例转换:

const combined = slicesRank.map((rank) => {
  const id = rank.id.replace("=rank", "");
  const count = slicesCount.find(
    (count) => count.id.replace("=count", "") === id
  );
  return {
    id,
    values: rank.values.map((val, index) => ({
      date: val.date,
      rank: val.measurement,
      count: count?.values[index].measurement
    }))
  };
});

Here's a working example https://codesandbox.io/s/awesome-lovelace-e31zj?file=/src/index.js这是一个工作示例https://codesandbox.io/s/awesome-lovelace-e31zj?file=/src/index.js

You can try this.你可以试试这个。

 let sliceRank=[{id: "Catan=rank", values: [{"date":"10/10/2020", "measurement":120} ]}]; let sliceCount=[{id: "Catan=count", values:[{"date":"10/10/2020", "measurement":20} ] }]; let newData=[]; const data = sliceRank.map((slice, sliceIndex) => { return { id:slice.id.split("=")[0], values: slice.values.map(({ date, measurement }, valueIndex) => ({ date, rank: measurement, count: sliceCount[sliceIndex].values.map(r=> r.measurement)[0], })) } }) console.log(data);

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

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