简体   繁体   English

如何连接两个对象数组?

[英]How to concatenate two object arrays?

I am working with a React native project and I want to concatenate two arrays (having the same form).我正在处理一个 React 本机项目,我想连接两个数组(具有相同的形式)。 First I am working with a section list and Redux;首先,我正在使用部分列表和 Redux; then I fetch data from a server 10 items by 10. So for example, my first array must be like this:然后我从服务器 10 个项目中获取数据 10 个。例如,我的第一个数组必须是这样的:

data = [
{title: '01/02/2020', data:[{id: 1,location: 'paris', city: 'paris'},{id: 2, location: 'london', city: 'london'},{id: 3,location: 'atalanta', city: 'atalanta'}]},
{title: '02/02/2020', data: [{id: 4,location: 'madrid', city: 'madrid'},{id: 5,location: 'miami', city: 'miami'}]}
]

At the second fetch, I have the problem of concatenation between the old and the new array.在第二次获取时,我遇到了旧数组和新数组之间的连接问题。 I want to merge those that have the same title grouped in one title, and all of the data inside one array.我想合并具有相同标题的那些分组在一个标题中的内容,以及一个数组中的所有数据。 For example, this is the second result from the second fetch:例如,这是第二次获取的第二个结果:

data2 = [
{title: '02/02/2020', data:[{id: 6,location: 'lesboa', city: 'lesboa'},{id: 7,location: 'amestrdam', city: 'amestrdam'},{id:8,location: 'roma', city: 'roma'}]},
{title: '03/02/2020', data: [{id: 9,location: 'milano', city: 'milano'},{id:10,location: 'tokyo', city: 'tokyo'}]}
]

I expect the result to be like this:我希望结果是这样的:

data3 = [
{title: '01/02/2020', data:[{id: 1,location: 'paris', city: 'paris'},{id: 2, location: 'london', city: 'london'},{id: 3,location: 'atalanta', city: 'atalanta'}]},
{title: '02/02/2020', data:[{id: 4,location: 'madrid', city: 'madrid'},{id: 5,location: 'miami', city: 'miami'},{id: 6,location: 'lesboa', city: 'lesboa'},{id: 7,location: 'amesterdam', city: 'amesterdam'},{id:8,location: 'roma', city: 'roma'}]},
{title: '03/02/2020', data: [{id: 9,location: 'milano', city: 'milano'},{id:10,location: 'tokyo', city: 'tokyo'}]}

I found code like this (below), but the problem is that it duplicates the object inside my data array every time, and sometimes it duplicates it to 54 times我找到了这样的代码(如下),但问题是它每次都复制我的数据数组中的对象,有时它复制到 54 次

case HISTORIQUE_SUCCESS:
      const {count, skip, groupedvalue} = action.payload;

      const data1 = [...state.data];

      let data = Object.values(
        [...data1, ...groupedvalue].reduce(
          (acc, curr) => ({
            ...acc,
            [curr.title]:
              curr.title in acc
                ? {...curr, data: [...acc[curr.title].data, ...curr.data]}
                : {...curr},
          }),
          {},
        ),
      );

      return {
        ...state,
        data: skip === 0 ? groupedvalue : data,
        count,
        loader: false,
      };

The script you have will take care of grouping by title , but not for keeping the objects unique by id .您拥有的脚本将负责按title分组,但不会按id保持对象的唯一性。

I tend to use Map for this purpose.为此,我倾向于使用Map This function will return a merged data array, given two input arrays.给定两个输入数组,此函数将返回合并的数据数组。 To demonstrate it, I have added the location with id 4 a in data2 also, for the same date as in data :为了演示它,我还在data2添加了 id 为 4 a 的位置,与data日期相同:

 function merged(data, data2) { let arr = data.concat(data2); let titles = new Map(arr.map(({title}) => [title, new Map])); arr.forEach(({title, data}) => { let map = titles.get(title); data.forEach(o => map.set(o.id, o)); }); return Array.from(titles.entries(), ([title, map]) => ({ title, data: [...map.values()] }) ); } let data = [{title: '01/02/2020',data:[{id: 1,location: 'paris', city: 'paris'},{id: 2, location: 'london', city: 'london'},{id: 3,location: 'atalanta', city: 'atalanta'}]},{title: '02/02/2020',data: [{id: 4,location: 'madrid', city: 'madrid'},{id: 5,location: 'miami', city: 'miami'}]}]; let data2 = [{title: '02/02/2020',data:[{id: 6,location: 'lesboa', city: 'lesboa'},{id: 4,location: 'madrid', city: 'madrid'},{id: 7,location: 'amestrdam', city: 'amestrdam'},{id:8,location: 'roma', city: 'roma'}]},{title: '03/02/2020',data: [{id: 9,location: 'milano', city: 'milano'},{id:10,location: 'tokyo', city: 'tokyo'}]}]; console.log(merged(data, data2));

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

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