简体   繁体   English

将两个不同的关联数组放在一个关联数组中作为一个数组,在javascript中有两个关联数组

[英]Put two different associative arrays inside one associative array as an array with two associative arrays in javascript

I Want to join two arrays into one array containing two different arrays First Array. 我想将两个数组连接成一个包含两个不同数组First Array的数组。 I do not mean a simple array but this time around a more complex array with field having the same values on both sides of the arrays. 我并不是指一个简单的数组,但这次是一个更复杂的数组,其中数组的两侧都有相同的值。

  var arr1 = [{ "id": "4", "ip_address": "127.0.0.1", "username": "superuser", "password": "$2y$08$awherOdjNPRoDHAiNBGZNuA92UGfT7jsIpsMMcNnyyJMxBA8Ug9q6", "salt": null, "email": "super@admin.com", "activation_code": null, "forgotten_password_code": "NULL", "forgotten_password_time": null, "remember_code": "cBjcajHj8qXaNrOhkAAqPe", "created_on": "2018-09-13", "last_login": "1540549332", "active": "1", "first_name": "Super", "last_name": "Admin", "phone": "0", "user_id": "4", "groups": [{ "id": "10", "name": "superusers", "description": "Super Administrators", "$$hashKey": "object:38" }], "$$hashKey": "object:11" }]; var arr2 = [{ "id": "1", "ip_address": "127.0.0.1", "username": "administrator", "password": "$2y$08$DoULTzDyGFyh.DTNOvxRtujA3CT2yVBMpp6joYnfUcD0FQgbm9rmy", "salt": "", "email": "admin@admin.com", "activation_code": "", "forgotten_password_code": null, "forgotten_password_time": null, "remember_code": "wYiqzg7AM2QbEPdVrqUhkO", "created_on": "2010-03-18", "last_login": "1537468397", "active": "1", "first_name": "Admin", "last_name": "istrator", "phone": "0", "user_id": "1", "groups": [{ "id": "3", "name": "admins", "description": "Administrators", "$$hashKey": "object:32" }], "$$hashKey": "object:8" }]; 

let's say you declared your arrays as arr1 and arr2 . 假设您将数组声明为arr1arr2 To merge them: 要合并它们:

var $users = arr1.concat(arr2);

If you want $users to be an array with two elements, and each of one being an array, you would do 如果你想让$ users成为一个包含两个元素的数组,并且每个元素都是一个数组,你就可以这样做

var $users = [arr1, arr2];

But that doesn't match your desired result and it would make little sense. 但这与你想要的结果不符,这没什么意义。

var a = [10, 20];
var b = [30, 40, 50];
Array.prototype.push.apply(a,b);
console.log(a);

 var arr1 = [{ "id": "4", "ip_address": "127.0.0.1", "username": "superuser", "password": "$2y$08$awherOdjNPRoDHAiNBGZNuA92UGfT7jsIpsMMcNnyyJMxBA8Ug9q6", "salt": null, "email": "super@admin.com", "activation_code": null, "forgotten_password_code": "NULL", "forgotten_password_time": null, "remember_code": "cBjcajHj8qXaNrOhkAAqPe", "created_on": "2018-09-13", "last_login": "1540549332", "active": "1", "first_name": "Super", "last_name": "Admin", "phone": "0", "user_id": "4", "groups": [{ "id": "10", "name": "superusers", "description": "Super Administrators", "$$hashKey": "object:38" }], "$$hashKey": "object:11" }]; var arr2 = [{ "id": "1", "ip_address": "127.0.0.1", "username": "administrator", "password": "$2y$08$DoULTzDyGFyh.DTNOvxRtujA3CT2yVBMpp6joYnfUcD0FQgbm9rmy", "salt": "", "email": "admin@admin.com", "activation_code": "", "forgotten_password_code": null, "forgotten_password_time": null, "remember_code": "wYiqzg7AM2QbEPdVrqUhkO", "created_on": "2010-03-18", "last_login": "1537468397", "active": "1", "first_name": "Admin", "last_name": "istrator", "phone": "0", "user_id": "1", "groups": [{ "id": "3", "name": "admins", "description": "Administrators", "$$hashKey": "object:32" }], "$$hashKey": "object:8" }]; let $users = arr1.concat(arr2); console.log($users); 

Concatenate both arrays using concat function 使用concat函数连接两个数组

const arr1 = [/\*values\*/];

const arr2 = [/\*values\*/];

// As in description: //如描述:

const $users = arr1.concat(arr2); //[firstValues, secondValues]

// As in title: //如标题所示:

const $users = [arr1, arr2]; //[[firstValues], [secondValues]]

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

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