Merge N arrays to 1 single array using JavaScript

huangapple go评论73阅读模式
英文:

Merge N arrays to 1 single array using javascript

问题

我正在尝试使用JavaScript将4个数组合并为一个单一数组,并且出现错误。

我想要以下结果:

{
  {name: 'sree', age:'33'},
  {name: 'kavitha', age:'34'},
  {name: 'darshan', gender:'Male'},
  {name: 'suchi', gender:'Female'}
}

我尝试使用concat方法,但出现错误,提示**.concat**不是一个函数。

英文:

I am trying to merge 4 arrays into 1 single array using javascript and throws error.

var dif1 = {name: 'sree', age:'33'};
var dif2 = {name: 'kavitha', age:'34'};
var dif3 = {name: 'darshan', gender:'Male'};
var dif4 = {name: 'suchi', gender:'Female'};

I want results like below

{
  {name: 'sree', age:'33'},
  {name: 'kavitha', age:'34'},
  {name: 'darshan', gender:'Male'},
  {name: 'suchi', gender:'Female'}
}

I have tried with concat method and it's throwing an error saying .concat is not a function.

答案1

得分: 1

那是4个对象,你正在尝试将对象放入一个没有键值对的对象中,如果你想要创建一个它们的数组,那么请使用以下代码。尝试运行片段以查看结果。

var dif1 = {name: 'sree', age:'33'};
var dif2 = {name: 'kavitha', age:'34'};
var dif3 = {name: 'darshan', gender:'Male'};
var dif4 = {name: 'suchi', gender:'Female'};

let arr = []

arr.push(dif1, dif2, dif3, dif4)
console.log(arr)
英文:

Those are 4 objects and you are trying to put objects in an object without a key value pair, and if you want to make a array of them then use below code. Try to run the snippet to see the result.
<!-- begin snippet: js hide: false console: true babel: true -->

<!-- language: lang-js -->

var dif1 = {name: &#39;sree&#39;, age:&#39;33&#39;};
var dif2 = {name: &#39;kavitha&#39;, age:&#39;34&#39;};
var dif3 = {name: &#39;darshan&#39;, gender:&#39;Male&#39;};
var dif4 = {name: &#39;suchi&#39;, gender:&#39;Female&#39;};

let arr = []

arr.push(dif1, dif2, dif3, dif4)
console.log(arr)

<!-- end snippet -->

答案2

得分: 0

你展示的内容不是数组,而是对象。对象是一个具有键和值的结构。你可能会感到困惑,因为在某些语言中,对象被称为关联数组

你可以使用concat()方法或展开操作符将这四个对象合并为一个数组。

// 方法1:使用concat()
var mergedArray = [].concat(dif1, dif2, dif3, dif4);
console.log(mergedArray);

// 方法2:使用展开操作符
var mergedArray = [...dif1, ...dif2, ...dif3, ...dif4];
console.log(mergedArray);

两种方法都会返回相同的值

[
  { name: 'sree', age: '33' },
  { name: 'kavitha', age: '34' },
  { name: 'darshan', gender: 'Male' },
  { name: 'suchi', gender: 'Female' }
]
英文:

What you are showing here are not arrays but these are objects. Object is a structure that has a key and a value. You might be confused because in some languages, objects are called associative arrays.

You can merge the four objects into one array using the concat() method or the spread operator.

// Method 1: Using concat()
var mergedArray = [].concat(dif1, dif2, dif3, dif4);
console.log(mergedArray);

// Method 2: Using the spread operator
var mergedArray = [...dif1, ...dif2, ...dif3, ...dif4];
console.log(mergedArray);

Both will return the same value

[
  { name: &#39;sree&#39;, age: &#39;33&#39; },
  { name: &#39;kavitha&#39;, age: &#39;34&#39; },
  { name: &#39;darshan&#39;, gender: &#39;Male&#39; },
  { name: &#39;suchi&#39;, gender: &#39;Female&#39; }
]

答案3

得分: 0

如果对象dif1、dif2、dif3、dif4是硬编码的,你可以一开始创建一个对象数组:

const obj_array = [
  {name: 'sree', age: '33'},
  {name: 'kavitha', age: '34'},
  {name: 'darshan', gender: 'Male'},
  {name: 'suchi', gender: 'Female'}
]

如果你想要在以后向数组中添加新对象:

obj_array.push({ name: 'a', gender: 'b' })

这里是除了push之外可用的数组方法:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#instance_methods

英文:

If the objects: dif1, dif2, dif3, dif4, are hardcoded you could create an array of objects to begin with:

const obj_array = [
  {name: &#39;sree&#39;, age:&#39;33&#39;},
  {name: &#39;kavitha&#39;, age:&#39;34&#39;},
  {name: &#39;darshan&#39;, gender:&#39;Male&#39;},
  {name: &#39;suchi&#39;, gender:&#39;Female&#39;}
]

and if you want to add new objects to the array later:

obj_array.push({ name: &#39;a&#39;, gender: &#39;b&#39; })

Here are all the methods that are available besides push on an array: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#instance_methods

答案4

得分: 0

第一种方法:

var dif1 = {name: 'sree', age: '33'};
var dif2 = {name: 'kavitha', age: '34'};
var dif3 = {name: 'darshan', gender: 'Male'};
var dif4 = {name: 'suchi', gender: 'Female'};

var obj = {...dif1, ...dif2, ...dif3, ...dif4};

第二种方法:

let newObj = Object.assign({}, dif1, dif2, dif3, dif4);
英文:

In terms of merging multiple objects into a single object, it can be done using two methods.

1st method:

var dif1 = {name: &#39;sree&#39;, age:&#39;33&#39;};
var dif2 = {name: &#39;kavitha&#39;, age:&#39;34&#39;};
var dif3 = {name: &#39;darshan&#39;, gender:&#39;Male&#39;};
var dif4 = {name: &#39;suchi&#39;, gender:&#39;Female&#39;};

var obj = {...diff1, ...dif2 ,...dif3 ,...dif4};

2nd method:

let newObj = Object.assign({}, diff1, dif2 , dif3, dif4);

答案5

得分: 0

{
{name: 'sree', age:'33'},
{name: 'kavitha', age:'34'},
{name: 'darshan', gender:'Male'},
{name: 'suchi', gender:'Female'}
}
不是一个对象,JavaScript对象是键值对结构的。
如果你想要合并成一个对象,你应该为每个数组添加键。
如果你想要合并成一个数组,你应该将{}改为[]。

英文:
{
{name: &#39;sree&#39;, age:&#39;33&#39;},
{name: &#39;kavitha&#39;, age:&#39;34&#39;},
{name: &#39;darshan&#39;, gender:&#39;Male&#39;},
{name: &#39;suchi&#39;, gender:&#39;Female&#39;}
} 

is not an object, javascript objects are key:value structred.
if you want to merge into one object, you should add keys for each array.
if you want to merge into one array, you should make the {} into [] .

huangapple
  • 本文由 发表于 2023年5月29日 15:31:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定