将多个单独的对象转换为 1 个对象数组
Turn multiple separate objects into 1 array of objects
我有两个哈希对象。
一个:
{"name" : "one" , "color" : "red" }
两个:
{"name" : "two" , "color" : "blue" }
我正在尝试把这两个对象变成这样的东西:
[{"name" : "one" , "color" : "red" }, {"name" : "two" , "color" : "blue" } ]
如何使用 javascript 实现此目的?
我也在用下划线。
此外 - 当我有新对象时,我会收到更多我想推送的单个对象 -
Three : {"name" : "three" , "color" : "red" }
结果为 -
Result : [{"name" : "one" , "color" : "red" }, {"name" : "two" , "color" : "blue" }, {"name" : "three" , "color" : "red" } ]
谢谢!
var a = {"name" : "one" , "color" : "red" };
var b = {"name" : "two" , "color" : "blue" };
var res = [a, b];
console.log(res);
或者像这样使用推送
var res = [];
res.push(a);
res.push(b);
试试这个:
var one = {"name" : "one" , "color" : "red" };
var two = {"name" : "two" , "color" : "blue" };
var array = [one, two];
或者像这样使用推送:
var array = [];
array.push(one);
//array is now equal to [one]
array.push(two);
//array is now equal to [one,two]
或者像这样使用 unshift:
var array = [];
array.unshift(one);
// array is now equal to [one]
array.unshift(two);
// array is now equal to [two, one]
基本上你只需要创建变量来捕获你的 Hash 对象值并使用你创建的变量来创建一个数组。
您可以使用其他值来执行此操作:
var three = {"name" : "three", "color" : "red" };
var array.push(three);
要显示所有值:
for (var i = 0; i < array.length; i++) {
console.log(array[i]);
}
如果对象数量未知,可以使用数组的[=f11=]函数:
var res = [];
var one = {"name" : "one" , "color" : "red" };
var two = {"name" : "two" , "color" : "blue" };
res.push(one);
res.push(two);
// res is now: [{"name" : "one" , "color" : "red" }, {"name" : "two" , "color" : "blue" }]
res.push({"name" : "three"});
// res is now: [{"name" : "one" , "color" : "red" }, {"name" : "two" , "color" : "blue" }, {"name" : "three"}]
我有两个哈希对象。
一个:
{"name" : "one" , "color" : "red" }
两个:
{"name" : "two" , "color" : "blue" }
我正在尝试把这两个对象变成这样的东西:
[{"name" : "one" , "color" : "red" }, {"name" : "two" , "color" : "blue" } ]
如何使用 javascript 实现此目的?
我也在用下划线。
此外 - 当我有新对象时,我会收到更多我想推送的单个对象 -
Three : {"name" : "three" , "color" : "red" }
结果为 -
Result : [{"name" : "one" , "color" : "red" }, {"name" : "two" , "color" : "blue" }, {"name" : "three" , "color" : "red" } ]
谢谢!
var a = {"name" : "one" , "color" : "red" };
var b = {"name" : "two" , "color" : "blue" };
var res = [a, b];
console.log(res);
或者像这样使用推送
var res = [];
res.push(a);
res.push(b);
试试这个:
var one = {"name" : "one" , "color" : "red" };
var two = {"name" : "two" , "color" : "blue" };
var array = [one, two];
或者像这样使用推送:
var array = [];
array.push(one);
//array is now equal to [one]
array.push(two);
//array is now equal to [one,two]
或者像这样使用 unshift:
var array = [];
array.unshift(one);
// array is now equal to [one]
array.unshift(two);
// array is now equal to [two, one]
基本上你只需要创建变量来捕获你的 Hash 对象值并使用你创建的变量来创建一个数组。
您可以使用其他值来执行此操作:
var three = {"name" : "three", "color" : "red" };
var array.push(three);
要显示所有值:
for (var i = 0; i < array.length; i++) {
console.log(array[i]);
}
如果对象数量未知,可以使用数组的[=f11=]函数:
var res = [];
var one = {"name" : "one" , "color" : "red" };
var two = {"name" : "two" , "color" : "blue" };
res.push(one);
res.push(two);
// res is now: [{"name" : "one" , "color" : "red" }, {"name" : "two" , "color" : "blue" }]
res.push({"name" : "three"});
// res is now: [{"name" : "one" , "color" : "red" }, {"name" : "two" , "color" : "blue" }, {"name" : "three"}]