2016-07-15 172 views
1

比方說,比如我有這個數組的數組:地圖陣列陣列到陣列

var arrayWithArrays = [["peter","director","40"],["marie","author","10"],["marie","author","6"],["peter","director","9"]]; 

我想要一個新的數組,都會有這樣的格式(唯一的名稱, 全部爲唯一的名稱小時加起來):

var chartData = [["peter", 49],["marie",16]]; 

我已經嘗試了分配的東西,但沒有按預期工作。 映射不起作用像預期

 var temp = _.map(arrayWithArrays ,function(x){ 

      if(chartData[x[0]]){ 
       chartData[x[0]] += parseInt(x[2]); 
      }else{ 
       chartData[x[0]] = parseInt(x[2]); 
      } 
     }); 

老式方法也犯規交付

 for (var x = 0; x < arrayWithArrays.length; x++) { 
      if(chartData.length != 0){ 
       for (var i = 0; i < chartData.length; i++) { 
        if(chartData[i][0] == arrayWithArrays[x][0]){ 
         chartData[i][2] += parseInt(arrayWithArrays[x][2]); 
        }else{ 
         var array = []; 
         array[0] = arrayWithArrays[x][0]; 
         array[1] = parseInt(arrayWithArrays[x][2]); 
         chartData.push(array); 
        } 
       } 
      }else{ 
       var array = []; 
       array[0] = arrayWithArrays[x][0]; 
       array[1] = parseInt(arrayWithArrays[x][2]); 
       chartData.push(array); 
      } 
     } 

我想我是得太多,我讓它更復雜更比它確實是...有人有一個想法讓我擺脫這種頭痛?我已經花了3個小時在此

+0

是否使用underscore.js其他解決辦法? – Teemu

+0

什麼是'chartData'?它在哪裏定義? – hindmost

+0

@Teemu是的,我用下劃線。 – MichaelAngelo

回答

3

您可以使用Array#forEach它並使用一個對象來引用插入的數組。

var arrayWithArrays = [["peter", "director", "40"], ["marie", "author", "10"], ["marie", "author", "6"], ["peter", "director", "9"]], 
 
    result = []; 
 

 
arrayWithArrays.forEach(function (a) { 
 
    if (!this[a[0]]) { 
 
     this[a[0]] = [a[0], 0]; 
 
     result.push(this[a[0]]); 
 
    } 
 
    this[a[0]][1] += +a[2]; 
 
}, Object.create(null)); 
 

 
console.log(result);

usersDatarolesData編輯成果:

var arrayWithArrays = [["peter", "director", "40"], ["marie", "programmer", "10"], ["peter", "author", "10"], ["peter", "author", "6"], ["peter", "director", "9"], ["marie", "author", "2"], ["marie", "author", "3"], ["marie", "programmer", "9"]], 
 
    usersData = [], 
 
    rolesData = []; 
 

 
arrayWithArrays.forEach(function (a) { 
 
    if (!this.user[a[0]]) { 
 
     this.user[a[0]] = { drilldown: a[0], name: a[0], y: 0, }; 
 
     usersData.push(this.user[a[0]]); 
 
    } 
 
    this.user[a[0]].y += +a[2];    
 
    if (!this.role[a[0]]) { 
 
     this.role[a[0]] = { load: { id: a[0], data: [] } }; 
 
     rolesData.push(this.role[a[0]].load); 
 
    } 
 
    if (!this.role[a[0]][a[1]]) { 
 
     this.role[a[0]][a[1]] = [a[1], 0]; 
 
     this.role[a[0]].load.data.push(this.role[a[0]][a[1]]); 
 
    } 
 
    this.role[a[0]][a[1]][1]+= +a[2]; 
 
}, { user: Object.create(null), role: Object.create(null) }); 
 

 
console.log(usersData); 
 
console.log(rolesData);

+0

我喜歡這個......「如何將數組映射到另一個數組」......「使用地圖?」 – evolutionxbox

+0

@evolutionxbox地圖.....!什麼「地圖」? :) – Jai

+0

對不起,應該使用forEach。 –

0

將溶液使用Array.forEachObject.keys功能:

var arrayWithArrays = [["peter","director","40"],["marie","author","10"],["marie","author","6"],["peter","director","9"]], 
 
    names = {}, result; 
 

 
arrayWithArrays.forEach(function (arr) { 
 
    (names[arr[0]])? names[arr[0]][1] += +arr[2] : names[arr[0]] = [arr[0], +arr[2]]; 
 
}); 
 
result = Object.keys(names).map(function (n) { 
 
    return names[n]; 
 
}); 
 

 
console.log(JSON.stringify(result, 0, 4));

0

這裏差不多,但與Array.prototype.reduceObject.keys

var arrayWithArrays = [ 
 
    ["peter", "director", "40"], 
 
    ["marie", "author", "10"], 
 
    ["marie", "author", "6"], 
 
    ["peter", "director", "9"] 
 
]; 
 

 
var obj = arrayWithArrays.reduce(function(acc, item) { 
 
    if (!acc[item[0]]) acc[item[0]] = 0; 
 
    acc[item[0]] += +item[2]; 
 
    return acc; 
 
}, {}); 
 

 
var newArr = Object.keys(obj).map(function(i) { 
 
    return [i, obj[i]]; 
 
}); 
 

 
console.log(newArr);