简体   繁体   English

如何使用 Ramda 表达式将包含 arrays 对象的数组转换为带有软管 object 的单个数组?

[英]How to convert an array containing arrays of objects to single array witht hose object using Ramda expressions?

I am having a difficult time, there is some bad mapping going on on my code.我遇到了困难,我的代码上有一些不好的映射。

I have an array containing array of objects like that:我有一个包含这样的对象数组的数组:

[
   [{a: 1, b: 2},{a: 1, b: 3} ],
   [{a: 5, b: 2},{a: 2, b: 5}]
]

And I want to make like that:我想这样做:

[
   {a: 1, b: 2},
   {a: 1, b: 3},
   {a: 5, b: 2},
   {a: 2, b: 5}
]

In order to do that, I thought I found the magical solution, make things flat, using flatten function, it was not working ( this problem is just a piece of code in a lot of code ) and I was wondering why, i wasted some time to find that this the problem, it is not the behovior I am expecting, as you can see in the image, the first thing I have is an array containing an array having two objects, with flatten method, I was expecting an array of two objects, but I am getting what you see in the image:为了做到这一点,我想我找到了神奇的解决方案,让事情变平,使用flatten function,它不起作用(这个问题只是很多代码中的一段代码)我想知道为什么,我浪费了一些是时候发现这个问题了,这不是我所期望的行为,正如您在图像中看到的那样,我拥有的第一件事是一个数组,其中包含一个具有两个对象的数组,使用flatten方法,我期待一个数组两个对象,但我得到了您在图像中看到的内容:

在此处输入图像描述

The code I have ttried is this:我试过的代码是这样的:

const expectedArray = R.flatten(myArrayOfArraysOfObjects);

Full example:完整示例:

const singleTronconPoints = troncon => {
  return troncon.geometri_linestring;
};
console.log('troncons : ');
console.log(troncons);
console.log('map troncons points');
console.log(map(singleTronconPoints, troncons));
console.log('flatten');
console.log(flatten(map(singleTronconPoints, troncons)));

and this is full result:这是完整的结果: 在此处输入图像描述

How can I solve that, is there another magical (:P ) solution ( method ) to solve the problem?我该如何解决,是否有另一个神奇的 (:P) 解决方案 ( method ) 来解决这个问题?

Any help would be much appreciated.任何帮助将非常感激。

Array.prototype.reduce() can also be an option: Array.prototype.reduce()也可以是一个选项:

const arr =[
  [{a: 1, b: 2},{a: 1, b: 3}],
  [{a: 5, b: 2},{a: 2, b: 5}]
]

const expectedArray = arr.reduce((acc, array) => {
  acc.push(...array);
  return acc;
}, []);

You can use array.flat您可以使用array.flat

 let a = [ [{ a: 1, b: 2 }, { a: 1, b: 3 }], [{ a: 5, b: 2 }, { a: 2, b: 5 }] ]; let b = a.flat(); console.log(b)

Alternatively you can use reduce and inside callback use forEach and puch items from the nested array to accumulator array或者,您可以使用reduce和内部回调使用forEach并将嵌套数组中的项目放入累加器数组

 let a = [ [{ a: 1, b: 2 }, { a: 1, b: 3 }], [{ a: 5, b: 2 }, { a: 2, b: 5 }] ]; let b = a.reduce((acc, curr) => { curr.forEach(item => acc.push(item)) return acc; }, []); console.log(b)

use reduce() + push which faster flat() method.使用reduce() + push 哪个更快的flat()方法。

refer this for to check performance.参考这个来检查性能。 : https://jsbench.me/0ikcqa83ck/1 : https://jsbench.me/0ikcqa83ck/1

 let arr = [ [{a: 1, b: 2},{a: 1, b: 3} ], [{a: 5, b: 2},{a: 2, b: 5}] ] console.log(arr.flat()) let flattenArr = arr.reduce((acc, val) => (acc.push(...val),acc), []) console.log(flattenArr);

Array.flat is the magical solution you are looking for ! Array.flat是您正在寻找的神奇解决方案!

 var arr = [ [{a: 1, b: 2},{a: 1, b: 3} ], [{a: 5, b: 2},{a: 2, b: 5}] ] console.log(arr.flat())

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

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