Combine two nested json objects with javascript and loop through it with a key

Viewed 577

I have a few json objects (obj1,obj2...) and each is taken from .txt files:

{
  "countries": [
    {
      "Country name": "China",
      "Flag": "CN",
      "Population": 1395380000,
      "undefined": "#688144"
    }, ... ]}

and

{
  "countries": [
    {
      "Country name": "India",
      "Flag": "IN",
      "Population": 1338677000,
      "undefined": "#B78A31"
    }, ...]}

And so on. Now I want to combine them like this:

{
  "countries": [
    {
      "Country name": "China",
      "Flag": "CN",
      "Population": 1395380000,
      "undefined": "#688144"
    },
 {
      "Country name": "India",
      "Flag": "IN",
      "Population": 1338677000,
      "undefined": "#B78A31"
    },
 ... ]}

So I can loop through data like this:

let obj1 = {}; //saved Data From Txt1;
let obj2 = {}; //saved Data From Txt2
...
let obj = combined?

for (var key in obj.countries) {
var num1 = obj.countries[key].Population+popholder;
if (target >= popholder && target <= num1) {
  var country = obj.countries[key]['Country name'];
  var testas = document.getElementById("countryname")
}}

How could I achieve this?

3 Answers

Just concat the arrays:

const obj1 = {
    "countries": [{
            "Country name": "China",
            "Flag": "CN",
            "Population": 1395380000,
            "undefined": "#688144"
        }
    ]
};

const obj2 = {
    "countries": [{
            "Country name": "India",
            "Flag": "IN",
            "Population": 1338677000,
            "undefined": "#B78A31"
        }
    ]
};

const result = {
    countries: [...obj1.countries, ...obj2.countries]
};

console.log(result);

You can use create a new object with the same countries properties and use Array#concat to combine all the arrays of countries into one:

const obj1 = {
  "countries": [{
    "Country name": "China",
    "Flag": "CN",
    "Population": 1395380000,
    "undefined": "#688144"
  }, /* ... */ ]
};

const obj2 = {
  "countries": [{
    "Country name": "India",
    "Flag": "IN",
    "Population": 1338677000,
    "undefined": "#B78A31"
  }, /* ... */ ]
};

const obj3 = {
  "countries": [{
    "Country name": "Sealand",
    "Flag": "",
    "Population": 27,
    "undefined": "#0000FF"
  }, /* ... */ ]
};

const combined = { 
  countries: [].concat(
    obj1.countries, 
    obj2.countries, 
    obj3.countries
  )
};

console.log(combined);

If you have an array of the objects, you can combine Array#map with spread syntax to extract the countries arrays and combine them into one:

const obj1 = { "countries": [{ "Country name": "China", "Flag": "CN", "Population": 1395380000, "undefined": "#688144" }, /* ... */ ]};
const obj2 = { "countries": [{ "Country name": "India", "Flag": "IN", "Population": 1338677000, "undefined": "#B78A31" }, /* ... */ ]};
const obj3 = { "countries": [{ "Country name": "Sealand", "Flag": "", "Population": 27, "undefined": "#0000FF" }, /* ... */ ]};

const objArr = [obj1, obj2, obj3];

const combined = { 
  combined: [].concat(
    ...objArr.map(x => x.countries)
  )
};

console.log(combined)

You can push all of the arrays into one.

const obj1 = {
  "countries": [
    {
      "Country name": "China",
      "Flag": "CN",
      "Population": 1395380000,
      "undefined": "#688144"
    }, ]}
const obj2 = {
  "countries": [
    {
      "Country name": "India",
      "Flag": "IN",
      "Population": 1338677000,
      "undefined": "#B78A31"
    },]};
const res = [obj1, obj2, /*...*/]
  .reduce((acc,{countries})=>(acc.countries.push(...countries),acc), {countries: []});
console.log(res);

Related