Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Concatenate two arrays of objects and remove repeated data from an attribute in JavaScript?
For this, use map() along with find(). Following is the code −
Example
var details1 =
[
{
productDetails:
{
isSold: true,
productId:101
}
},
{
productDetails:
{
isSold: true,
productId:103
}
}
]
var details2 =
[
{
productDetails:
{
isSold: false,
productId:101
}
}
]
var details3 = details1.map(details1Object=>{
var newObject= details2.find(obj=>obj.productDetails.productId
=== details1Object.productDetails.productId)
return newObject? newObject : details1Object
})
console.log(details3)
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo183.js.
Output
This will produce the following output −
PS C:\Users\Amit\javascript-code> node demo183.js
[
{ productDetails: { isSold: false, productId: 101 } },
{ productDetails: { isSold: true, productId: 103 } }
]Advertisements