10

I am using Typescript for below problem. I want to search object not simple alphabetic or number in the list.

Below are the two arrays. I want to get common objects in separate list without using any third party library.

    firstArray = [
           {
               "id": 4,
               "name": "Tata"
           },
           {
               "id": 11,
               "name": "Maruti"
           },
           {
               "id": 14,
               "name": "Mahindra"
           }
        ]

        secondArray = [
           {
               "id": 4,
               "name": "Tata"
           },
           {
               "id": 11,
               "name": "Maruti"
           },
           {
               "id": 15,
               "name": "Hyundai"
           },
           {
               "id": 21,
               "name": "Honda"
           } 
        ]
        
// Get Common Elements
// I am getting blank array as output

        console.log(firstArray.filter(( make ) => secondArray.includes( make)));

Is there good function or way to find out commons element?

Morez
  • 2,048
  • 5
  • 36
  • 49
  • 3
    Unless you use types and have a question related to them it's a **Javascript** question. – Mörre May 02 '18 at 12:58
  • @Mörre i was just trying out available options anyway I want to make this in typescript only. – Morez May 02 '18 at 13:02
  • 2
    Possible duplicate of [javascript object array difference and intersection of elements](https://stackoverflow.com/questions/33356504/javascript-object-array-difference-and-intersection-of-elements) – Toodoo May 02 '18 at 13:04
  • @Ronnie Does not matter, it's Javascript (ECMAScript). – Mörre May 02 '18 at 21:30

3 Answers3

26

You can use array#filter with array#some. For each object in the first array, check if id and name exist in the other array.

const firstArray = [{ "id": 4, "name": "Tata" }, { "id": 11, "name": "Maruti" }, { "id": 14, "name": "Mahindra" } ], 
      secondArray = [{ "id": 4, "name": "Tata" }, { "id": 11, "name": "Maruti" }, { "id": 15, "name": "Hyundai" }, { "id": 21, "name": "Honda" } ],
      result = firstArray.filter(o => secondArray.some(({id,name}) => o.id === id && o.name === name));
console.log(result);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
1

For ES6, you can also try sets,

For demonstration,

const thirdArray = [...new Set([...firstArray ,...secondArray])];
Mohib Wasay
  • 311
  • 2
  • 13
0

In case, someone is wishing that extra parameters of 2nd array should be merged into the first array then here is the code:


const firstArray = [{ "id": 4, "name": "Tata" }, { "id": 11, "name": "Maruti" }, { "id": 14, "name": "Mahindra" }]

const secondArray = [{ "id": 4, "name": "Tata", "new": 1, junk: "as" }, { "id": 11, "name": "Maruti", "new": 2, junk: "as1" }, { "id": 15, "name": "Hyundai", "new": 3, junk: "asdds" }, { "id": 21, "name": "Honda", junk: "asasd" }]

const newArr =[]
const result = firstArray.filter(o => secondArray.some((sitm) => o.id === sitm.id && newArr.push({
    new: sitm.id,
    //add more fields if needed
    ...o
})));

console.log("*** final result ***", newArr);

Note: result is the just the common part of both arrays and newArr is common part plus the attributes you need from second array.