2016-11-04 113 views
2

我有一個數組,看起來像:從兩個陣列組合值在JavaScript

var data = [{"year":[1981],"weight":[3]}, 
      {"year":[1982],"weight":[4]}, 
      {"year":[1985],"weight":[7]}] 

我的數據系列,1980年開始,以一年1986年結束我的任務是輸入所有缺失值到數組;在我的情況下,最終陣列應該是:

var data = [{"year":[1980],"weight":[0]}, 
      {"year":[1981],"weight":[3]}, 
      {"year":[1982],"weight":[4]}, 
      {"year":[1983],"weight":[0]}, 
      {"year":[1984],"weight":[0]}, 
      {"year":[1985],"weight":[7]}, 
      {"year":[1986],"weight":[0]}] 

我在兩個步驟中實現了這個任務。首先,我創建了一個長度爲七個元素(1980 - 1986年)的空數組,並初始化每個元素的值爲{"year": $CURRENT_YEAR, "weight": 0}。然後,我循環訪問data數組,在空數組中找到當前年份的索引,並用當前值替換yearweight字段。我的代碼粘貼在下面。

我不知道代碼是否可以用更優雅的方式重寫。

// Create empty array 
var my_array = [] 
var length = 7 

// 1st step 
year = 1980 
for (var i = 0; i < length; i++) { 
    my_array.push({"year": year, "weight": 0}); 
    year++ 
} 

// 2nd step 
for (var j = 0; j < data.length; j++) { 
    curr_year = data[j]["year"][0]; 
    curr_weight = data[j]["weight"][0] 
    var index = my_array.findIndex(function(item, i) {return item.year === curr_year}) 
    my_array[index] = {"year": curr_year, "weight": curr_weight} 
} 
+0

爲什麼在陣列中的值,如[1981]和[3]?你打算存儲更多的價值嗎?我發現你的代碼在第一步中並沒有將值存儲爲數組,但是它在第二步中部分地完成了。所以二者之一是錯誤的。你的代碼不能像那樣工作。 – trincot

+0

「我想知道代碼是否可以用更優雅的方式重寫。」 - 如果這是你的問題,是不是應該發佈到[codereview](http://codereview.stackexchange.com/)? – evolutionxbox

+0

那麼,代碼顯然沒有做OP的要求,所以我認爲這個問題真的應該改寫爲*「這是行不通的,因爲我得到了這個結果(....),而不是所期望的結果(...)。 我做錯了什麼?」*。不適用於CodeReview。 – trincot

回答

2

這是最好的.map()做這個工作再說,如果你有一個大的輸入數組它可能是明智的建立首先如哈希(LUT);

var data = [{"year":[1981],"weight":[3]}, 
 
      {"year":[1982],"weight":[4]}, 
 
      {"year":[1985],"weight":[7]}], 
 
    lut = data.reduce((p,c) => p[c.year[0]] ? p : (p[c.year[0]] = c, p), {}); 
 
    range = [1980,1986], 
 
    result = Array(range[1]-range[0] + 1).fill() 
 
             .map((_,i) => lut[i+range[0]] ? lut[i+range[0]] : {year: [i+range[0]], weight: [0]}); 
 
console.log(result);

1

您可以組合2路和做兩個步驟在一個循環中的數組元素的

// Create empty array 
var my_array = [] 
var length = 7 


year = 1980 
for (var i = 0; i < length; i++) { 
    // check if there is data for the year 
    var index = data.findIndex(function(item, i) {return item.year === year}); 
    if(index > -1){ //if there is data, use it 
     my_array.push({"year": data[index]["year"][0], "weight": data[index]["weight"][0]}); 
    }else{ //put in default data 
     my_array.push({"year": year, "weight": 0}); 
    } 
    year++; 
} 
1

查找指數每一次對大數據的糟糕表現。我可以建議如下算法:

// Create empty object and fill it with values where keys are years 
var years = {}; 
data.forEach(item => { 
    years[item.year[0]] = item.weight[0]; 
}); 

// Result array with all years 
var result = []; 
var startYear = 1980; 
var endYear = 1986; 

// Generate our result array 
for (var i = startYear; i <= endYear; i++) { 

    // If property for given year (i) exists in "years" object then add it to "result" array 
    // in other case add default object with weight 0 
    var o = years[i] ? { year: [i], weight: [years[i]] } : { year: [i], weight: [0] }; 
    result.push(o); 
} 
1

你可以只用find()while循環做到這一點。

var data = [{"year":[1981],"weight":[3]},{"year":[1982],"weight":[4]},{"year":[1985],"weight":[7]}]; 
 
        
 
var i = 1980; 
 
var result = []; 
 

 
while(i <= 1986) { 
 
    var find = data.find(e => e.year[0] == i); 
 
    (find) ? result.push(find) : result.push({year: [i], weight: [0]}); 
 
    i++; 
 
} 
 

 
console.log(result)

你也可以先用map()獲得的年數組,然後使用while循環與indexOf()

var data = [{"year":[1981],"weight":[3]},{"year":[1982],"weight":[4]},{"year":[1985],"weight":[7]}]; 
 
      
 
var i = 1980; 
 
var result = []; 
 
var years = data.map(e => e.year[0]); 
 

 
while(i <= 1986) { 
 
    var ind = years.indexOf(i); 
 
    (ind != -1) ? result.push(data[ind]) : result.push({year: [i], weight: [0]}); 
 
    i++; 
 
} 
 

 
console.log(result)