ES6:将两个数组合并成一个对象数组 [英] ES6: Merge two arrays into an array of objects

查看:4557
本文介绍了ES6:将两个数组合并成一个对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



第一个数组是日期(字符串):

  let metrodates = [
2008-01,
2008-02,
2008 -03,.. ect
];

第二个数组是数字:

  let figures = [
0,
0.555,
0.293,... ect
]

我想将它们合并成一个这样的对象(所以数组项通过相似的索引来匹配):

  let metrodata = [
{data:0,date:2008-01},
{data:0.555,date: 2008-02},
{data:0.293,date:2008-03},.. ect
];

到目前为止,我这样做:我创建一个空数组,然后循环通过第一个两个数组来获取索引号(前两个数组是相同的长度)...但是有一个更简单的方法(在ES6中)?

 code> let metrodata = []; 

for(let index in metrodates){
metrodata.push({data:figures [index],date:metrodates [index]});
}


解决方案

最简单的方法是使用映射和提供给回调的索引



  let metrodates = [2008-01,2008-02,2008-03]; let figures = [0,0.555,0.293]; let output = metrodates.map((date ,i)=>({date,data:figures [i]})); console.log(output);  

/ p>




另一个选项是制作一个通用的 zip 函数,将两个输入数组整理成一个阵列。这通常被称为zip,因为它将输入像拉链上的牙齿交错。



  const zip =([x,... xs],[y,... ys])=> {if(x === undefined || y === undefined)return []; else return [[x,y],... zip(xs,ys)];} let metrodates = [2008-01,2008-02,2008-03]; let figure = [0, 0.555,0.293]; let output = zip(metrodates,figures).map(([date,data])=>({date,data})); console.log(output);  






另一个选项是使一个通用的 map 函数,它接受多个源数组。映射函数将从每个源列表中接收一个值。请参阅 Racket的地图程序,以获取更多的使用示例。



这个答案似乎是最复杂的,但也是最通用的它接受任何数量的源数组输入。



  const isEmpty = xs => ; xs.length === 0; const head =([x,... xs])=> x; const tail =([x,... xs])=> xs; const map =(f,... xxs)=> {let loop =(acc,xxs)=> {if(xxs.some(isEmpty))return acc; else return loop([... acc,f(... xxs.map(head))],xxs.map(tail)); }; return loop([],xxs);} let metrodates = [2008-01,2008-02,2008-03]; let figure = [0,0.555,0.293]; let output = map日期,数据)=>({date,data}),metrodates,figures); console.log(输出);  

/ p>

I have two arrays that I want to merge together to one array of objects...

The first array is of dates (strings):

let metrodates = [
 "2008-01",
 "2008-02",
 "2008-03",..ect
];

The second array is of numbers:

let figures = [
 0,
 0.555,
 0.293,..ect
]

I want to merge them to make an object like this (so the array items match up by their similar index):

let metrodata = [
   {data: 0, date: "2008-01"},
   {data: 0.555, date: "2008-02"},
   {data: 0.293, date: "2008-03"},..ect
];

So far I do this like so: I create an empty array and then loop through one of the first two arrays to get the index number (the first two arrays are the same length)... But is there an easier way (in ES6)?

  let metrodata = [];

  for(let index in metrodates){
     metrodata.push({data: figures[index], date: metrodates[index]});
  }

解决方案

The easiest way is probably to use map and the index provided to the callback

let metrodates = [
  "2008-01",
  "2008-02",
  "2008-03"
];

let figures = [
  0,
  0.555,
  0.293
];

let output = metrodates.map((date,i) => ({date, data: figures[i]}));

console.log(output);


Another option is to make a generic zip function which collates your two input arrays into a single array. This is usually called a "zip" because it interlaces the inputs like teeth on a zipper.

const zip = ([x,...xs], [y,...ys]) => {
  if (x === undefined || y === undefined)
    return [];
  else
    return [[x,y], ...zip(xs, ys)];
}

let metrodates = [
  "2008-01",
  "2008-02",
  "2008-03"
];

let figures = [
  0,
  0.555,
  0.293
];

let output = zip(metrodates, figures).map(([date, data]) => ({date, data}));

console.log(output);


Another option is to make a generic map function which accepts more than one source array. The mapping function will receive one value from each source list. See Racket's map procedure for more examples of its use.

This answer might seem the most complicated but it is also the most versatile because it accepts any number of source array inputs.

const isEmpty = xs => xs.length === 0;
const head = ([x,...xs]) => x;
const tail = ([x,...xs]) => xs;

const map = (f, ...xxs) => {
  let loop = (acc, xxs) => {
    if (xxs.some(isEmpty))
      return acc;
    else
      return loop([...acc, f(...xxs.map(head))], xxs.map(tail));
  };
  return loop([], xxs);
}

let metrodates = [
  "2008-01",
  "2008-02",
  "2008-03"
];

let figures = [
  0,
  0.555,
  0.293
];

let output = map(
  (date, data) => ({date, data}),
  metrodates,
  figures
);

console.log(output);

这篇关于ES6:将两个数组合并成一个对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆