1

Lets say for example i have this array with arrays:

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

I want a new array that will have this format (unique name, all the hours for the unique name added up):

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

I have tried allot of things but nothing works as expected. Mapping doesnt work like expected

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

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

Old fashioned way also doesnt deliver

        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);
            }
        }

I think i am overthinking it and i make it more complex than it really is...someone has an idea to get me out of this headache ? I already spent 3 hours over this

3
  • Are you using underscore.js? Commented Jul 15, 2016 at 7:58
  • What is chartData? Where is it defined? Commented Jul 15, 2016 at 7:59
  • @Teemu yes i use underscore. Commented Jul 15, 2016 at 8:00

3 Answers 3

3

You could use Array#forEach for it and use an object for the reference to the inserted array.

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);

Edit for results in usersData and rolesData:

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);

Sign up to request clarification or add additional context in comments.

8 Comments

I love this... "How to map array to another array" ... "use map?"
sorry, it should be use forEach.
@NinaScholz works perfectly! I am trying to figure out how to read your code so that i can create a second array that looks like this ["peter","peter",["director",2]]. (Counting how many times role occurs.) should i create another loop or can it be done in the same one again ?
i suggest to change the data structure to an object. there is a better access to the properties, and it's better readable. to the question: as result, you want some arrays, filled with the name of the same role, and at last, an array with role and count?
example: var chartData = [["peter", 49,["director",2,"actor",4]],["marie",16],["roleName",count]]; it should take a unique role and count how many times it occured
|
0

The solution using Array.forEach and Object.keys functions:

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));

Comments

0

Similar to other solutions here but with Array.prototype.reduce and Object.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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.