使用键 x 和 y 从 2 个数组创建一个数组

Create one array from 2 Arrays with keys x & y

我有 2 个数组 xDatesyMentions

xDates

[1453766400000, 1453852800000, 1453939200000...

y提及

[5160, 5240, 7090...

目标是一个像这样的数组:

[
    {
       x: 1453766400000,
       y: 5160
    },
    ...
]

尝试使用 Ramda Zip 认为 zipObj 将是我需要的,但以下只产生 1 个对象:

R.zipObj(['x', 'x', 'x'], [1, 2, 3]); => {"x": 3}

我想也许我 运行 R.zipObj 在 x 然后是 y 数组,然后将它们压缩在一起,然后将其设置为下面 mentionsPointsArray 的数组:

const createMentionPoints = (frequencyPoints, termsData) => {
    const yMentions = termsData.mentions;
    const propX = R.prop('x');
    const xPointsFromFrequency = R.map(propX, frequencyPoints);
    console.log('xDates', xPointsFromFrequency)
    console.log('yMentions', yMentions)
    const mentionsPointsArray = []
    return frequencyPoints;
};

你应该使用 Array#map 函数。

map() 方法创建一个新数组,其中包含在此 array 中的每个元素上调用提供的函数的结果。提供的函数是 callback

结果数组中的元素是对象,如下所示:{"x":item, "y":yMentions[i]}.

var xDates=[1453766400000, 1453852800000, 1453939200000];
var yMentions=[5160, 5240, 7090];
console.log(xDates.map(function(elem,i){
    return {"x":elem,"y":yMentions[i]}
}));

The ramda solution http://ramdajs.com/docs/#zipWith

var createPoints = (x, y) => {
  return { x: x, y: y }
};

R.zipWith(createPoints, [1, 2, 3], ['a', 'b', 'c']);

// returns: [{"x": 1, "y": "a"}, {"x": 2, "y": "b"}, {"x": 3, "y": "c"}]

我认为最干净的 point-free 版本是:

const data1 = ['a', 'b', 'c']
const data2 = [1, 2, 3]

R.zipWith(R.objOf, data1, data2)

请查看工作 REPL here