JavaScript 推送到数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5173441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript push to array
提问by Hyman
How do I push new values to the following array?
如何将新值推送到以下数组?
json = {"cool":"34.33","alsocool":"45454"}
I tried json.push("coolness":"34.33");
, but it didn't work.
我试过了json.push("coolness":"34.33");
,但没有用。
回答by Lightness Races in Orbit
It's not an array.
它不是一个数组。
var json = {"cool":"34.33","alsocool":"45454"};
json.coolness = 34.33;
or
或者
var json = {"cool":"34.33","alsocool":"45454"};
json['coolness'] = 34.33;
you could do it as an array, but it would be a different syntax (and this is almost certainly not what you want)
你可以把它作为一个数组来做,但它会是一种不同的语法(这几乎肯定不是你想要的)
var json = [{"cool":"34.33"},{"alsocool":"45454"}];
json.push({"coolness":"34.33"});
Note that this variable name is highly misleading, as there is no JSON here. I would name it something else.
请注意,此变量名称具有很强的误导性,因为此处没有 JSON。我会把它命名为别的东西。
回答by jenson-button-event
var array = new Array(); // or the shortcut: = []
array.push ( {"cool":"34.33","also cool":"45454"} );
array.push ( {"cool":"34.39","also cool":"45459"} );
Your variable is a javascript object {}
not an array []
.
您的变量是一个 javascript 对象{}
而不是一个数组[]
。
You could do:
你可以这样做:
var o = {}; // or the longer form: = new Object()
o.SomeNewProperty = "something";
o["SomeNewProperty"] = "something";
and
和
var o = { SomeNewProperty: "something" };
var o2 = { "SomeNewProperty": "something" };
Later, you add those objects to your array: array.push (o, o2);
稍后,您将这些对象添加到数组中: array.push (o, o2);
Also JSON
is simply a string representation of a javascript object, thus:
也JSON
只是一个 javascript 对象的字符串表示,因此:
var json = '{"cool":"34.33","alsocool":"45454"}'; // is JSON
var o = JSON.parse(json); // is a javascript object
json = JSON.stringify(o); // is JSON again
回答by James Sumners
That is an object, not an array. So you would do:
那是一个对象,而不是一个数组。所以你会这样做:
var json = { cool: 34.33, alsocool: 45454 };
json.supercool = 3.14159;
console.dir(json);
回答by u476945
object["property"] = value;
or
或者
object.property = value;
Object and Array in JavaScript are different in terms of usage. Its best if you understand them:
JavaScript 中的 Object 和 Array 在用法上是不同的。如果您了解它们,那就最好了:
回答by Jaydeep Gondaliya
Use the push()
function to append to an array:
使用该push()
函数附加到数组:
// initialize array
var arr = [
"Hi",
"Hello",
"Bonjour"
];
// append new value to the array
arr.push("Hola");
Now array is
现在数组是
var arr = [
"Hi",
"Hello",
"Bonjour"
"Hola"
];
// append multiple values to the array
arr.push("Salut", "Hey");
Now array is
现在数组是
var arr = [
"Hi",
"Hello",
"Bonjour"
"Hola"
"Salut"
"Hey"
];
// display all values
for (var i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
Will print:
将打印:
Hi
Hello
Bonjour
Hola
Salut
Hey
Update
更新
If you want to add the items of one array to another array, you can use Array.concat:
如果要将一个数组的项添加到另一个数组,可以使用Array.concat:
var arr = [
"apple",
"banana",
"cherry"
];
arr = arr.concat([
"dragonfruit",
"elderberry",
"fig"
]);
console.log(arr);
Will print
会打印
["apple", "banana", "cherry", "dragonfruit", "elderberry", "fig"]