English 中文(简体)
j 关键用途和价值作为阵列
原标题:javascript object key and value as array

i want to create an object dynamically of the form - {"abc": [x1,x2], "efg": [x3, x4, x1]} The following code is not working.. what s the problem here ?

var catCmp = {};
var x1="abc";
var x2="efg";

var y1="x1";
var y2="x2";
var y3="x3";
var y4="x4";

if (typeof catCmp[x1] ===  undefined ) {
    catCmp[x1] = [];
}
if (typeof catCmp[x2] ===  undefined ) {
    catCmp[x2] = [];
}

catCmp[x1] = catCmp[x1].push(y1);
catCmp[x1] = catCmp[x1].push(y2);
catCmp[x2] = catCmp[x2].push(y3);
catCmp[x2] = catCmp[x2].push(y4);
catCmp[x2] = catCmp[x2].push(y1);

console.log( catCmp ::: , catCmp);
问题回答

你们不需要放弃推动行动的结果。 页: 1

如下:

catCmp[x1] = catCmp[x1].push(y1); 

由<代码>catCmp[x1]push(y1)退还的价值为y1。 因此,这是分配给<代码>catCmp[x1]的价值。

正如其他答复中所建议的那样,没有做转让,只是:

catCmp[x1].push(y1); 
catCmp[x1].push(y1);
catCmp[x1].push(y2);
catCmp[x2].push(y3);
catCmp[x2].push(y4);
catCmp[x2].push(y1);




相关问题
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.

热门标签