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

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

问题描述

我有两个数组,我想将它们合并到一个对象数组中...

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
];

第二个数组是数字:

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
];

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

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]});
  }

推荐答案

最简单的方法可能是使用 map 和提供给回调的索引

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);

另一种选择是创建一个通用的 zip 函数,将您的两个输入数组整理成一个数组.这通常被称为拉链",因为它像拉链上的牙齿一样交错输入.

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);

另一种选择是创建一个通用的 ma​​p 函数,它接受多个源数组.映射函数将从每个源列表接收一个值.见 Racket 的映射过程,了解更多使用示例.

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天全站免登陆