Push nested JSON values to array

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

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)

Push nested json values to array using node js, I need to show my json results into nested array format: { “state”: [ { “stateName”: “​tamilnadu” } ], “city”: [ { “cityName”: “chennai” } ] } This is my  You can make use of Array.push method to push a JSON object to an array list. let list = []; let myJson = { "name" : "sam" } list.push(myJson); console.log(list) Let’s look at another use case where you have to create a JSON object dynamically from an array and push to another array.

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)

Nested JSON: How to add (push) new items to an object?, javascript object json array of objects create json object create nested json object in javascript dynamically. I'm just starting with Arrays, Objects, and JSON - so  You need to create an empty array between the two for-loops (scoreRow) and push the value into that array each time you increase c. Then each time you increase r scores.push (scoreRow).

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)), []);

How to access nested json objects in JavaScript?, Accessing nested json objects is just like accessing nested arrays. Nested objects are the objects that are inside an another object.In the  Solved: Jeez! Why is this so tough to get! :) Need the 'results' array nested here: { "d": { " results ": [

How to access nested JSON object Inside Array in react js, JSON (JavaScript Object Notation) is a lightweight data-interchange format for Javascript. It is primarily used for transmitting data between a web  Hello All, I would like to have some assistance on how to add a nested field array to a new field based on the key string value. As you can see in the json output below, I am simply trying to add the nested field [properties][additionalDetails][value]] if "key" = "invitedUserEmailAddress" I have tried: filter { if "invitedUserEmailAddress" in [properties][additionalDetails] { mutate { add

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

JSON Arrays, In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array Nested Arrays in JSON Objects. Values in an array can  Extracting optional values from JSON array of nested JSON objects Sometimes you want to extract values from a JSON array and transform them into a KNIME table such that one row corresponds to a single value of the array. If the JSON array is nested, i.e., if it contains JSON objects or arrays itself, you sometimes do not want to extract its full values, but only parts thereof. These values can

accessing nested JSON array data in Zapier Webhook, How can we access nested JSON array elements in a Zapier Webhook?Given this incoming JSON  Parsing Nested JSON Data in JavaScript. JSON objects and arrays can also be nested. A JSON object can arbitrarily contains other JSON objects, arrays, nested arrays, arrays of JSON objects, and so on. The following example will show you how to parse a nested JSON object and extract all the values in JavaScript.

how to update specific nested json object? : GameSparks, hi. thanks. very helpful tutorial. helped us so much. i need a little more info. how to add an entry to an array object? and then how to update an existing entry? i  First, we don't support doT.js array syntax directly, that is to say, that you can't write presenter like {{~it.array:value:index}} and you need use presenter and column value format callback together to do that.

JSON methods, toJSON, The JSON (JavaScript Object Notation) is a general format to represent gets every key/value pair including nested objects and array items. As my Title, I'd like to get parent data from nested json. I know how to do it in HTML. Update: If you want, push parent name into children array and access it

Comments
  • Your output isnt clear. Can you elaborate it.
  • There is no such thing as a JSON Object.
  • updated with clearer output
  • Have you tried anything? Seems like a loop over the array and a few push statements would do the trick...
  • Possible duplicate of From an array of objects, extract value of a property as array