Home > database >  Creating new array and removing unwanted objects from old arrays javascipt
Creating new array and removing unwanted objects from old arrays javascipt

Time:04-18

I have 2 arrays containing objects, I want to create a new array without "duplicates". The problem is that the two arrays are not identical and I want to choose based on id. This is a simple version of what I got:

let array1 = [
    {
        "id": "1",
        "type": "abc"
    },
    {
        "id": "2",
        "type": "one, two, three"
    }
]

let array2 = [
    {
        "id": "1",
        "type": "abc",
        "country": "England"
    },
    {
        "id": "3",
        "type": "one, two, three",
        "country": "Germany"
    }
]

let array3 = array1.filter(x => array2.forEach(y => x.id === y.id)).concat(array2);

The result I want for array3 is:

{
        "id": "1",
        "type": "abc"
 }

In this particular case country is not important and will be thrown away anyway.

How do I filter out unwanted objects and create a new array with only the objects I want?

CodePudding user response:

If you are looking for an intersection between the arrays the shortest answer is to use the filter with a find.

let array1 = [{
    id: '1',
    type: 'abc',
  },
  {
    id: '2',
    type: 'one, two, three',
  },
];

let array2 = [{
    id: '1',
    type: 'abc',
    country: 'England',
  },
  {
    id: '3',
    type: 'one, two, three',
    country: 'Germany',
  },
];

const array3 = array1.filter(value => array2.find((a)=>a.id==value.id));

console.log(array3);

CodePudding user response:

let array1 = [{
    id: '1',
    type: 'abc',
  },
  {
    id: '2',
    type: 'one, two, three',
  },
];

let array2 = [{
    id: '1',
    type: 'abc',
    country: 'England',
  },
  {
    id: '3',
    type: 'one, two, three',
    country: 'Germany',
  },
];

array2.forEach((el, idx) => {
  const match = array1.find((el2) => el2.id === el.id);
  if (!match) array1.push(el);
});

console.log(array1);

Something like this ? I assumed from your explanation that you want to merge array2 with array1. You will end up with the unique objects from array2 having the extra country property, if you want to delete that you need an extra step before pushing.

CodePudding user response:

I think this is what you were trying to achieve:

let array1 = [ { "id": "1", "type": "abc" }, { "id": "2", "type": "one, two, three" } ]; 
let array2 = [ { "id": "1", "type": "abc", "country": "England" }, { "id": "3", "type": "one, two, three", "country": "Germany" } ];

let array3 = array1.filter(x => array2.map(y => y.id).includes(x.id));

console.log( array3 );

In order to avoid have to recompute the array2 array of ids you can get them in advance as follows:

let array1 = [ { "id": "1", "type": "abc" }, { "id": "2", "type": "one, two, three" } ]; 
let array2 = [ { "id": "1", "type": "abc", "country": "England" }, { "id": "3", "type": "one, two, three", "country": "Germany" } ];

const ids = array2.map(({id}) => id);
let array3 = array1.filter(x => ids.includes(x.id));

console.log( array3 );

  • Related