English 中文(简体)
以指数x取代所有阵列的体
原标题:Replace all strings at index x in nested arrays

我的阵列就是这样:

[["1", "apple 1"], ["1", "pear 2"], ["2", "lemon 1"], ["12", "lemon 12"]]

我愿分别以<代码>one”代替1、2和12个阵列指数0和one>、、2“>“twelve”

[["one", "apple 1"], ["one", "pear 2"], ["two", "lemon 1"], ["twelve", "lemon 12"]]

我如何这样做?

最佳回答
var arr = [
    [  1 ,  apple 1  ], [  2 ,  pear 2  ], [  3 ,  lemon 3  ]
]

var numbers = [  one ,  two ,  three  ]

arr.forEach(function (el) {
    el[0] = numbers[el[0]]
})

arr // [ [  one ,  apple 1  ], [  two ,  pear 2  ], [  three ,  lemon 3  ] ]

射线指数实际上是插图,为什么 编号[2]( 2_ a string)将检索第三名成员。

在阵列上,可使用for-loop,但

问题回答

例如:

var obj = [["1", "apple 1"], ["1", "pear 2"], ["2", "lemon 1"], ["12", "lemon 12"]];

for ( i = 0; i < obj.length ; i++ ) {
    switch(obj[i][0]) {
        case "1" : obj[i][0] = "one"; break;
        case "2" : obj[i][0] = "two"; break;
        case "12" : obj[i][0] = "twelve"; break;
    }
}

http://jsfiddle.net

<><>Edit>>

我想提一下的是,这一解决办法要求你把阵列起来:。 Arr = JSON.stringification(theArr).replace(.)。 页: 1

我可以最快的是:

var str =  [["1", "apple 1"], ["1", "pear 2"], ["2", "lemon 1"], ["12", "lemon 12"]] .replace(/([")12/g, $1twelve ).replace(/([")2/g, $1two ).replace(/([")1/g, $one );

But there are a couple of catches here: if write the replace(/([")12/g, $1twelve ) after you ve replaced the ones, it won t work. so to get around this, you could do:

str =  [["1", "apple 1"], ["1", "pear 2"], ["2", "lemon 1"], ["12", "lemon 12"]] .replace(/([")12"/g, $1twelve" ).replace(/([")2"/g, $1two" ).replace(/([")1"/g, $one" );

添加最后引文。 然而,更可取的解决办法是:

var replace = { 1 : one , 2 : two , 12 : twelve };

并将相应的数字作为财产名称。





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