1

I have an array of objects that look similar to this,

 [{
        name : Client 1,
        total: 900,
        value: 12000
    }, {
        name : Client 2,
        total: 10,
        value: 800
    }, {
        name : Client 3,
        total: 5,
        value : 0
}]

What I am wanting is to get 3 arrays from this, an array of name,

[Client 1, Client 2, Client 3]

and array of totals,

[900, 10, 5]

and an array of values,

[12000, 800, 0]

I thought I would be able to something like map or similar but I am very confused about how to use it. Can anyone help me out?

Udders
  • 6,914
  • 24
  • 102
  • 194

4 Answers4

2

Use Array.prototype.map function

var arr = [{ name: "Client 1", total: 900, value: 12000 }, { name: "Client 2", total: 10, value: 800 }, { name: "Client 3", total: 5, value: 0 }];

var totals = arr.map(e => e.total);
var names = arr.map(e => e.name);
var values = arr.map(e => e.value);

document.write("<pre>" + totals + "</pre>");
document.write("<pre>" + names + "</pre>");
document.write("<pre>" + values + "</pre>");

Note from @Andy

This is ES6 so you might need a transpiler if your browsers don't support it yet.

isvforall
  • 8,768
  • 6
  • 35
  • 50
1

If you're fine with an object holding each of the arrays, the following Array.prototype.reduce will work:

var a = [{
        name : "Client 1",
        total: 900,
        value: 12000
    }, {
        name : "Client 2",
        total: 10,
        value: 800
    }, {
        name : "Client 3",
        total: 5,
        value : 0
}];

var res = a.reduce(function(a,b){
    return {
    name: a.name.concat(b.name),
    total: a.total.concat(b.total),
    value: a.value.concat(b.value)
  }
},{
    name: [],
    total:[],
    value:[]
})

console.log(res) // Object {name: Array[3], total: Array[3], value: Array[3]}
baao
  • 71,625
  • 17
  • 143
  • 203
0

You can use an object with the wanted keys as arrays.

var data = [{ name: 'Client 1', total: 900, value: 12000 }, { name: 'Client 2', total: 10, value: 800 }, { name: 'Client 3', total: 5, value: 0 }],
    result = function (array) {
        var r = {};
        array.forEach(function (a) {
            Object.keys(a).forEach(function (k) {
                r[k] = r[k] || [];
                r[k].push(a[k]);
            });
        });
        return r;
    }(data);
 
document.write('<pre>name: ' + JSON.stringify(result.name, 0, 4) + '</pre>');
document.write('<pre>total: ' + JSON.stringify(result.total, 0, 4) + '</pre>');
document.write('<pre>value: ' + JSON.stringify(result.value, 0, 4) + '</pre>');
document.write('<pre>the object: ' + JSON.stringify(result, 0, 4) + '</pre>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

if i uderstood you right you need to create 3 arrays from 1? You can do somethink like this:

var names=[];
var totals=[];
var values=[];

for(var i=0; i<objectArray.length; i++){
  names.push(objectArray[i].name);
  totals.push(objectArray[i].total);
  values.push(objectArray[i].value);
}
alexey
  • 783
  • 1
  • 7
  • 19