3

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?

4
  • 1
    What do you mean by combine? What is the expected result? Commented Aug 3, 2020 at 16:36
  • Any duplicates in those arrays? Or are they all unique? Can you give a minimal reproducible example of what the input and expected output looks like? Commented Aug 3, 2020 at 16:54
  • @VLAZ all data is unique Commented Aug 3, 2020 at 17:00
  • @hev1 I edited my question Commented Aug 3, 2020 at 17:00

3 Answers 3

3

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

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

Comments

2

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)

Comments

1

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

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.