将数组映射到数组中

时间:2016-07-15 07:54:30

标签: javascript dictionary

让我们说例如我有这个数组的数组:

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个小时来完成这个

3 个答案:

答案 0 :(得分:3)

您可以使用Array#forEach并使用对象来引用插入的数组。

&#13;
&#13;
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);
&#13;
&#13;
&#13;

usersDatarolesData中修改结果:

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

使用Array.forEachObject.keys函数的解决方案:

&#13;
&#13;
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));
&#13;
&#13;
&#13;

答案 2 :(得分:0)

与此处的其他解决方案类似,但Array.prototype.reduceObject.keys

&#13;
&#13;
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);
&#13;
&#13;
&#13;