[英]how to merge array inside another array objects in one array in javascript
I am looking to merge array inside another array objects in one array in javascript.我希望将数组合并到 javascript 中一个数组中的另一个数组对象中。 I tried many way like concat and join methods but I couldn't get it I have array below array which I need to merge into one array
我尝试了很多方法,如 concat 和 join 方法,但我无法得到它我在数组下面有数组,我需要合并到一个数组中
arr = [
[{
PRODUCE: 'Apple',
STATE: 'CA',
COUNTRY: 'US'
},
{
PRODUCE: 'Apple',
STATE: 'CA',
COUNTRY: 'US'
}
],
[{
PRODUCE: 'Apple',
STATE: 'CP',
COUNTRY: 'US'
}]
]
I want result as below我想要的结果如下
[
{
PRODUCE: 'Apple',
STATE: 'CA',
COUNTRY: 'US'
},
{
PRODUCE: 'Apple',
STATE: 'CA',
COUNTRY: 'US'
},
{
PRODUCE: 'Apple',
STATE: 'CP',
COUNTRY: 'US'
}
]
You can use flat() for this您可以为此使用flat()
The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.
flat() 方法创建一个新数组,其中所有子数组元素递归连接到指定深度。
Here you can read more about it在这里你可以阅读更多关于它的信息
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
const arr = [ [{ PRODUCE: 'Apple', STATE: 'CA', COUNTRY: 'US' }, { PRODUCE: 'Apple', STATE: 'CA', COUNTRY: 'US' } ], [{ PRODUCE: 'Apple', STATE: 'CP', COUNTRY: 'US' }] ] console.log(arr.flat());
You can use Array.flat()
.您可以使用
Array.flat()
。
var newArray = arr.flat([depth]);
var newArray = arr.flat([深度]);
const arr = [ [{ PRODUCE: 'Apple', STATE: 'CA', COUNTRY: 'US' }, { PRODUCE: 'Apple', STATE: 'CA', COUNTRY: 'US' } ], [{ PRODUCE: 'Apple', STATE: 'CP', COUNTRY: 'US' }] ] console.log(arr.flat())
If you are able to use modern javascript, you can try:如果您能够使用现代 javascript,您可以尝试:
arr.flat()
More info here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat更多信息在这里: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
If not, you can just use lodash, underscore or ramda.如果没有,您可以只使用 lodash、下划线或 ramda。 Libraries with useful utilities.
具有有用实用程序的库。
https://lodash.com/docs/4.17.15#flatten https://lodash.com/docs/4.17.15#flatten
There are two flat() answer, but alternative code that you tried for concat(), you can try like this:有两个 flat() 答案,但是您为 concat() 尝试的替代代码,您可以这样尝试:
var arr = [ [{ PRODUCE: 'Apple', STATE: 'CA', COUNTRY: 'US' }, { PRODUCE: 'Apple', STATE: 'CA', COUNTRY: 'US' } ], [{ PRODUCE: 'Apple', STATE: 'CP', COUNTRY: 'US' }] ] var newArray = arr[1].concat(arr[0]); console.log(newArray);
Simple Approach, using spread operator and forEach loop简单的方法,使用扩展运算符和 forEach 循环
const arr = [ [{ PRODUCE: 'Apple', STATE: 'CA', COUNTRY: 'US' }, { PRODUCE: 'Apple', STATE: 'CA', COUNTRY: 'US' } ], [{ PRODUCE: 'Apple', STATE: 'CP', COUNTRY: 'US' }] ] let result = []; arr.forEach((item)=>{ if(Array.isArray(item)){ result = [...result,...item] } else{ result = [...result,item] } } ) console.log(result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.