0

I have a JSON array of the following type:

"team": [ 
          {
            "paid": {
                "refugee": 2018,
                  "local": 29000, 
                  "international": 12000
            }
        },
        {
            "unpaid": {
                "refugee": 2019,
                "local": 39000, 
                "international": 19000
            }
        }
    ]

I would like to push the values of matching keys into an array, so that I end up with the following new arrays:

var refugees = [2018, 2019]
var local = [29000, 39000]
var international = [12000, 19000]

and so on..

What would be a simple method of doing this? I have succesfully used jQuery in the past for this but need a Javascript only solution:

$.each(team, function (i, v) {
                var teams = v;
                console.log(teams);
                $.each(v, function (i, v) {
                    refugees.push(v.refugee);
                    local.push(v.local);
                    international.push(v.international);
                });
            });
Dale K
  • 25,246
  • 15
  • 42
  • 71
ogot
  • 341
  • 2
  • 17

4 Answers4

1

Try this

var a={"team" : [ 
  {
    "paid": {
        "refugee": 2018,
          "local": 29000, 
          "international": 12000
    }
},

{
    "unpaid": {
        "refugee": 2019,
        "local": 39000, 
        "international": 19000
    }
}
]}
var refugee=[];
var local=[];
var international=[];
a.team.map((e)=>{
  if(e.paid)
  {
refugee.push(e.paid.refugee);
local.push(e.paid.local);
international.push(e.paid.international)
  }
  else
  {
    refugee.push(e.unpaid.refugee);
local.push(e.unpaid.local);
international.push(e.unpaid.international)
  }

})
console.log(local)
console.log(international)
console.log(refugee)
ellipsis
  • 12,049
  • 2
  • 17
  • 33
0

You can use reduce.

So here the idea is we are taking a the keys and mapping it to output object. we keep checking if the key is already their in output object we push the value to that particular key and if not we add a new property with value.

let obj = {"team":[{"paid":{"refugee":2018,"local":29000,"international":12000}},{"unpaid":{"refugee":2019,"local":39000,"international":19000}}]}

 let op = obj.team.reduce((output,current)=>{
  let temp = Object.values(current)[0]
  let values = Object.keys(temp)
  values.forEach(ele=>{
    if(output[ele]){
      output[ele].push(temp[ele])
    } else {
      output[ele] = [temp[ele]]
    }
  })
  return output;
 }, {})
 
 console.log(op)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
0

Something like this would work if you wanted a few one-liners:

let local = team.reduce((acc, item) => acc.concat(Object.values(item).map(val => val.local)), []);

let refugee = team.reduce((acc, item) => acc.concat(Object.values(item).map(val => val.refugee)), []);
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Val Geyvandov
  • 95
  • 1
  • 4
-1

Use Array#reduce, Object#values, Object#entries, spread syntax, destructuring and Map

const data={"team":[{"paid":{"refugee":2018,"local":29000,"international":12000}},{"unpaid":{"refugee":2019,"local":39000,"international":19000}}]}

const res = data.team.reduce((a,c)=>{
  Object.values(c)
  .map(Object.entries)
  .flat()
  .forEach(([k,v])=>{
    const arr = a.get(k) || [];
    arr.push(v)
    a.set(k, arr);
  })
  return a;
}, new Map())

//get all
console.log([...res.values()]);
//get by type
console.log(res.get('refugee'));
console.log(res.get('local'));
console.log(res.get('international'));
kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131