skip to Main Content

By default, I have arrays with the name of the continents.

europe= [];
northAmerica = [];
southAmerica = [];
asia = [];

And i have two arrays with some data

arr1 = [1,3];

arr2 = [
  { country: "Brazil", id: 1, continent: "southAmerica" },
  { country: "Germany", id: 2, continent: "europe" },
  { country: "India", id: 3, continent: "asia" },
  { country: "China", id: 4, continent: "asia" },
];

If the id matches in the arrays, I need to send the string obj.country to the corresponding array of the continent.

I tried like this:

arr2.map((item) => {
        if (arr1.find((id) => id === item.id)) {
          if (item.continent === "southAmerica") {
            southAmerica.push(item.country);
          }
          if (item.continent === "asia") {
            asia.push(item.country);
          }
        }
      });

But only the last value is sent to the arrays.

3

Answers


  1. const arr1Set = new Set(arr1);
    let myArr = []
    // Check if any object in arr2 has an ID that is also in arr1
    const containsIdenticalIds = arr2.some(obj => {
        if (arr1Set.has(obj.id)) {
            myArr.push(obj.country)
        }
    });
    
    
    console.log(myArr); // Output: true
    
    Login or Signup to reply.
  2. You want to use an object to make accessing the arrays a lot easier. You want to use forEach and includes.

    const continents = {
      europe: [],
      northAmerica: [],
      southAmerica: [],
      asia: [],
    };
    
    const arr1 = [1,3];
    
    const arr2 = [
      { country: "Brazil", id: 1, continent: "southAmerica" },
      { country: "Germany", id: 2, continent: "europe" },
      { country: "India", id: 3, continent: "asia" },
      { country: "China", id: 4, continent: "asia" },
    ];
    
    arr2.forEach(data => {
      if (arr1.includes(data.id)) {
        continents[data.continent].push(data.country);
      }
    });
    
    console.log(continents);

    using reduce

    const arr1 = [1,3];
    
    const arr2 = [
      { country: "Brazil", id: 1, continent: "southAmerica" },
      { country: "Germany", id: 2, continent: "europe" },
      { country: "India", id: 3, continent: "asia" },
      { country: "China", id: 4, continent: "asia" },
    ];
    
    const continents = arr2.reduce((continents, data) => {
      if (arr1.includes(data.id)) {
        continents[data.continent].push(data.country);
      }
      return continents;
    }, {
      europe: [],
      northAmerica: [],
      southAmerica: [],
      asia: []
    });
    
    console.log(continents);
    Login or Signup to reply.
  3. Do it like this.

    const continents = {
      europe: [],
      northAmerica: [],
      southAmerica: [],
      asia: []
    };
    
    
    arr1 = [1, 3];
    
    arr2 = [
      { country: "Brazil", id: 1, continent: "southAmerica" },
      { country: "Germany", id: 2, continent: "europe" },
      { country: "India", id: 3, continent: "asia" },
      { country: "China", id: 4, continent: "asia" },
    ];
    
    arr1.forEach(_id => {
      arr2.find(obj => {
        if (obj.id === _id) {
          continents[obj.continent].push(obj.country);
          return;
        }
      });
    });
    
    console.log(continents);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search