0

I'd like to remove duplicate object. How can I do this?

const array1 = [{ currencyName : "USD", code: "121" }, 
                { currencyName : "INR", code: "123" }];

const array2 = [{ currencyName : "USD", code: "121" }];

Result = [{ currencyName : "INR", code: "121" }]
1

2 Answers 2

0

Try to use filter and some methods:

const array1 =[ 
    { currencyName : "USD", code: "121" }, 
    { currencyName : "INR", code: "123" }
]

const array2=[ { currencyName : "USD", code: "121" }];

const result = array1.filter(f=> 
    !array2.some(s=> f.code === s.code && f.currencyName === s.currencyName)
);

console.log(result)
Sign up to request clarification or add additional context in comments.

Comments

0

const array1 = [{
  currencyName: "USD",
  code: "121"
}, {
  currencyName: "INR",
  code: "123"
}, ]

const array2 = [{
  currencyName: "USD",
  code: "121"
}, {
  currencyName: "FRA",
  code: "122"
}]

let array1Uniques = array1.filter(a => !array2.some(b => b.code === a.code));
let array2Uniques = array2.filter(a => !array1.some(b => b.code === a.code));
let result = [...array1Uniques,  ...array2Uniques];
console.log(result);

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.