数组详细信息
array1 = [{name:'kumar', id:3},{name:'jack', id:4},{name:'jhonny', id:5}];
array2 = [{name:'simon', id:11},{name:'david', id:12},{name:'jack', id:13}];
我需要这样的数组结果...
result={array1:[{name:'kumar', id:3},{name:'jack', id:4},{name:'jhonny', id:5}]
array2 :[{name:'simon', id:11},{name:'david', id:12},{name:'jack', id:13}]
}
我尝试过concat,但没有得到想要的结果...
答案 0 :(得分:0)
如果我理解正确,那么您实际要做的是将两个数组放入一个对象中,而数组的名称成为该对象中的键?
鉴于:
array1 = [...]
array2 = [...]
这样做:
{ array1, array2 }
Note: this is the same as doing { array1: array1, array2: array2 }
应该给您:
{
array1: [...],
array2: [...]
}
答案 1 :(得分:0)
您可以简单地声明性地这样做。
var array1 = [{name:'kumar', id:3},{name:'jack', id:4},{name:'jhonny', id:5}];
var array2 = [{name:'simon', id:11},{name:'david', id:12},{name:'jack', id:13}];
var result = {array1: array1, array2: array2};
console.log(result)
答案 2 :(得分:0)
只需将每个项目添加到所需的对象中,如下所示:
var array1 = [{
name: 'kumar',
id: 3
}, {
name: 'jack',
id: 4
}, {
name: 'jhonny',
id: 5
}];
var array2 = [{
name: 'simon',
id: 11
}, {
name: 'david',
id: 12
}, {
name: 'jack',
id: 13
}];
var result = {
array1,
array2
};
console.log(result);
答案 3 :(得分:0)
该结构是一个包含2个数组的对象:
array1 = [{name:'kumar', id:3},{name:'jack', id:4},{name:'jhonny', id:5}];
array2 = [{name:'simon', id:11},{name:'david', id:12},{name:'jack', id:13}];
var result = {array1: array1, array2: array2 };
console.log (result);
答案 4 :(得分:0)
实际上,它是一个有两个键的json对象,其中两个由数组值组成。
let array1 = [{name:'kumar', id:3},{name:'jack', id:4},{name:'jhonny', id:5}];
let array2 = [{name:'simon', id:11},{name:'david', id:12},{name:'jack', id:13}];
let result={
array1 : array1,
array2 : array2
}
输出:
{
"array1":[{"name":"kumar","id":3},{"name":"jack","id":4},{"name":"jhonny","id":5}],
"array2":[{"name":"simon","id":11},{"name":"david","id":12},{"name":"jack","id":13}]
}
如果需要它作为字符串,请使用JSON.stringify(result);转换为字符串。
答案 5 :(得分:0)
声明一个名称结果对象,并添加任意数量的键,例如 result.key
var array1 = [{name:'kumar', id:3},{name:'jack', id:4},{name:'jhonny', id:5}];
var array2 = [{name:'simon', id:11},{name:'david', id:12},{name:'jack', id:13}];
var result = {};
result.array1 = array1;
result.array2 = array2;
console.log(result)