English 中文(简体)
贾瓦伦语推向阵列
原标题:JavaScript push to array

我如何把新的价值观推向以下阵列?

json = {"cool":"34.33","alsocool":"45454"}

I tried json.push("coolness":"34.33");, but it didn t work.

最佳回答

它不是一个阵列。

var json = {"cool":"34.33","alsocool":"45454"};
json.coolness = 34.33;

or

var json = {"cool":"34.33","alsocool":"45454"};
json[ coolness ] = 34.33;

你可以把它作为一个阵列来,但会是一个不同的集团(这几乎肯定不是你想要的东西)。

var json = [{"cool":"34.33"},{"alsocool":"45454"}];
json.push({"coolness":"34.33"});

请注意,这一可变名称令人非常误导,因为there is no JSON here。 我将把它称作其他东西。

问题回答
var array = new Array(); // or the shortcut: = []
array.push ( {"cool":"34.33","also cool":"45454"} );
array.push (  {"cool":"34.39","also cool":"45459"} );

页: 1

你可以做到:

var o = {}; // or the longer form: = new Object()
o.SomeNewProperty = "something";
o["SomeNewProperty"] = "something";

以及

var o = { SomeNewProperty: "something" };
var o2 = { "SomeNewProperty": "something" };

后来,你在座标上添加这些物体:array.push(o, o2);

此外,/code>只是对 j物体的描述性表述,因此:

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

这是一个目标,而不是一个阵列。 因此,你将:

var json = { cool: 34.33, alsocool: 45454 };
json.supercool = 3.14159;
console.dir(json);
object["property"] = value;

object.property = value;

Java的标语和阿雷尔语在使用上有所不同。 如果你理解,最好:

Object vs Array: JavaScript

使用https://developer.mozilla.org/en-US/docs/Web/Java/Reference/Global_Objects/Array/push"rel=“nofollow”>push()功能,与阵列对应:

// initialize array
var arr = [
    "Hi",
    "Hello",
    "Bonjour"
];

// append new value to the array
arr.push("Hola");

现在阵列

var arr = [
    "Hi",
    "Hello",
    "Bonjour"
    "Hola"
];

// append multiple values to the array
arr.push("Salut", "Hey");

现在阵列

var arr = [
    "Hi",
    "Hello",
    "Bonjour"
    "Hola"
    "Salut"
    "Hey"
];

// display all values
for (var i = 0; i < arr.length; i++) {
    console.log(arr[i]);
}

页: 1

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:

var arr = [
    "apple",
    "banana",
    "cherry"
];

arr = arr.concat([
    "dragonfruit",
    "elderberry",
    "fig"
]);

console.log(arr);

Will print

["apple", "banana", "cherry", "dragonfruit", "elderberry", "fig"]




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签