2015-08-03 97 views
0

我需要的是這樣的:創建多個dimesion陣列

var fruits[1] = ["Banana", "Orange", "Apple", "Mango"]; 
fruits[2] = ["Banana"]; 
fruits[1].push("Lemon"); 
fruits[2].push("Orange"); 

但這不是在JavaScript工作。

有沒有辦法在javascript中實現這種機制?

回答

6

首先,初始化fruits本身作爲數組

var fruits = []; 
 
fruits[1] = ["Banana", "Orange", "Apple", "Mango"]; 
 
fruits[2] = ["Banana"]; 
 
fruits[1].push("Lemon"); 
 
fruits[2].push("Orange"); 
 

 
console.log(fruits);

+0

我有一個問題:有沒有這樣的東西:水果[1] .remove(「檸檬」)? –

+1

http://stackoverflow.com/questions/5767325/remove-a-specific-element-from-an-array @TolgayToklar – AmmarCSE

1

你可以做到以下幾點:

var fruits = []; //declared a blank array 

fruits.push("Banana"); //adding 1st item 
fruits.push("Orange"); //adding 2nd item 
fruits.push("Apple"); //adding 3rd item 
fruits.push("Mango"); //adding 4th item 

//if you want to update any item then simply do the following 
fruits[2] = "Lemon"