2016-09-22 83 views
0

什麼是存儲的值在位置的陣列,讓我們說,[30] [1] [0] ...保存價值

我試過最快捷的方式var myArray[30][1][0] = new Date();但事先不做var myArray = []; myArray[30] = []; myArray[30][1] = [];不會起作用。

我在做什麼錯?

編輯:我的問題是,我不知道,首先,如果變量myArray已經存在,第二,如果已經有位置值,比如說[29] [2] [15],哪個我不想覆蓋。

+0

你爲什麼試圖給一個多維數組賦值一個日期? –

+0

簡短回答:因爲我在一個NodeJs後端管理數百個日期,需要將其作爲JSON發送到前端,然後返回到NodeJS服務器。 – Emilio

回答

0

正確的方法做你想做的是什麼:

var array = [[[new Date()]]]; 

然而,由此產生多維數組不會有第二第三層面正好是30元和1,就像你在你原來有碼。不知道這是否會對您造成問題。

當你這樣做myArray[30][1][0],JavaScript是試圖在陣列myArray在訪問元素30,但由於myArray變量沒有被初始化爲任何事情,它具有價值undefined。所以,你的代碼是等效採用:

var myArray; 
myArray[30] = new Date(); 

該代碼將發出錯誤Cannot set property '30' of undefined

編輯: 如果你想確保數組分配值之前就存在,並避免重寫現有的值,我能想到的最優雅的方式是:

// Store a value in the array, at index (posX, posY, posZ), only if 
// that position in the array is currently empty 
function storeDateNoOverride(theArray, posX, posY, posZ, date) { 
    var theArray = theArray || []; 
    theArray[posX] = theArray[posX] || []; 
    theArray[posX][posY] = theArray[posX][posY] || []; 
    theArray[posX][posY][posZ] = theArray[posX][posY][posZ] || date; 
} 

// If variable theArray doesn't have a value yet, assign it to a new array 
var theArray = theArray || []; 

storeDateNoOverride(theArray, 30, 1, 0, new Date()); 
+0

不,這不是問題,但我的問題是,我不知道,首先,如果變量數組已經存在,其次,如果已經有一個位置的值,假設[29] [2] [15 ],我不想覆蓋。 – Emilio

+0

@Emilio:請參閱我的編輯(添加示例代碼,檢查現有值) – AdrianT

+0

非常感謝!這真太了不起了! – Emilio

-1

不能初始化一個嵌套的對象像那。有兩種方法。如果你事先知道你的ND數組大小,你應該首先將它實例化,如下所示:

Array.prototype.clone = function(){ 
 
    return this.map(e => Array.isArray(e) ? e.clone() : e); 
 
}; 
 
function arrayND(...n){ 
 
    return n.reduceRight((p,c) => c = (new Array(c)).fill().map(e => Array.isArray(p) ? p.clone() : p)); 
 
} 
 

 
var arr = arrayND(5,5,5,void 0); // create 5x5x5 array and fill each cell with "undefined" 
 
arr[3][2][1] = arr[3][2][1] || new Date(); // if falsey value then insert current date. 
 
console.log(arr);

但是,如果你不事先知道多維大小,並想增加你的動態數組,你可以使用函數像getNestedValue()setNestedValue()。在這種情況下,我將擴大Object.prototype。在生產代碼中,這應該通過Object.defineProperty()工具來完成。

Object.prototype.getNestedValue = function(...a) { 
 
    return a.length > 1 ? (this[a[0]] !== void 0 && this[a[0]].getNestedValue(...a.slice(1))) : this[a[0]]; 
 
}; 
 

 
Object.prototype.setNestedValue = function(...a) { 
 
    a.length > 2 ? typeof this[a[0]] === "object" && this[a[0]] !== null ? this[a[0]].setNestedValue(...a.slice(1)) 
 
                     : (this[a[0]] = typeof a[1] === "string" ? {} : new Array(a[1]), 
 
                     this[a[0]].setNestedValue(...a.slice(1))) 
 
       : this[a[0]] = a[1]; 
 
    return this; 
 
}; 
 

 
var arr = []; 
 
arr.getNestedValue(30,1,0) || arr.setNestedValue(30,1,0,new Date()); // if exists don't overwrite it; 
 
console.log(arr[30][1][0]);

+0

非常感謝!會考慮! – Emilio

0

什麼是存儲在位置數組中的值,比方說最快捷的方式,[30] [1] [0] ...

經典的方式是

array[30] = array[30] || []; 
array[30][1] = array[30][1] || []; 
array[30][1][0] = array[30][1][0] || []; 
array[30][1][0].push(value); 

或等效的適當pa rameterized時尚。

你可以做一個小幫手:

function subarray(array, n) { 
    return array[n] = array[n] || []; 
} 

然後,你可以寫

subarray(subarray(subarray(array, 30), 1), 0).push(value) 

你也可以寫的幫手來完成所有的訪問,並在一個檢查一舉:

function getNestedElement(array, firstIndex, ...indices) { 
    if (firstIndex === undefined) return array; 
    return getNestedElement(array[firstIndex] = array[firstIndex] || [], ...indices); 
} 

然後寫

getNestedElement(array, 30, 1, 0).push(value) 
+0

非常感謝!會考慮! – Emilio