How to find Objects Containing Matching Elements from Arrays in Another Object ?
Last Updated :
07 Feb, 2024
The task is to find objects that contain matching elements from arrays in another object. An object containing an array of values and a separate object. The objective is to determine whether the values present in the array are also present as keys in the separate object.
These are the following methods:
Using forEach loop
The forEach executes a provided function once for each array element. It does not return a new array. It is used to iterate through each ID in productIDs.ids and check if it exists in the productDetails object, pushing the corresponding product details into the matchingProducts array.
Example: Finding details of the products matching with IDs in the array using the forEach.
Javascript
const productIDs = {
ids: [101, 102, 104, 105]
};
const productDetails = {
101: { name: "Laptop" , price: 999 },
102: { name: "Smartphone" , price: 599 },
103: { name: "Headphones" , price: 99 },
104: { name: "Tablet" , price: 299 },
106: { name: "Mouse" , price: 29 }
};
function findProductsByID(productIDs, productDetails) {
const matchingProducts = [];
productIDs.ids.forEach(id => {
if (productDetails.hasOwnProperty(id)) {
matchingProducts.push(productDetails[id]);
}
});
return matchingProducts;
}
const matchingProducts = findProductsByID(productIDs,
productDetails);
console.log( "Matching products:" , matchingProducts);
|
Output
Matching products: [
{ name: 'Laptop', price: 999 },
{ name: 'Smartphone', price: 599 },
{ name: 'Tablet', price: 299 }
]
Using filter() and map() method
The filter() method creates a new array with all elements that pass the test implemented by the provided function. The findProductsByID function first uses filter to filter out the IDs that exist in the productDetails object. Then, the map() is used to map the filtered IDs to their corresponding product details.
Example: Finding details of the products matching with ids in the array using the filter and map method.
Javascript
const productIDs = {
ids: [101, 102, 104, 105]
};
const productDetails = {
101: { name: "Laptop" , price: 999 },
102: { name: "Smartphone" , price: 599 },
103: { name: "Headphones" , price: 99 },
104: { name: "Tablet" , price: 299 },
106: { name: "Mouse" , price: 29 }
};
function findProductsByID(productIDs,
productDetails) {
return productIDs.ids
.filter(id => productDetails
.hasOwnProperty(id))
.map(id => productDetails[id]);
}
const matchingProducts = findProductsByID(productIDs,
productDetails);
console.log( "Matching products:" ,
matchingProducts);
|
Output
Matching products: [
{ name: 'Laptop', price: 999 },
{ name: 'Smartphone', price: 599 },
{ name: 'Tablet', price: 299 }
]
Using reduce() method
The reduce() method is used to accumulate the matching products. It iterates over each ID in the productIDs.ids array and checks if the ID exists in the productDetails object. If it does, it adds the corresponding product details to the accumulator (matchingProducts).
Example: Fnding details of the products matching with ids in the array using the reduce method.
Javascript
const productIDs = {
ids: [101, 102, 104, 105]
};
const productDetails = {
101: { name: "Laptop" , price: 999 },
102: { name: "Smartphone" , price: 599 },
103: { name: "Headphones" , price: 99 },
104: { name: "Tablet" , price: 299 },
106: { name: "Mouse" , price: 29 }
};
function findProductsByID(productIDs,
productDetails) {
return productIDs.ids
.reduce((matchingProducts, id) => {
if (productDetails.hasOwnProperty(id)) {
matchingProducts
.push(productDetails[id]);
}
return matchingProducts;
}, []);
}
const matchingProducts = findProductsByID(productIDs,
productDetails);
console.log( "Matching products:" , matchingProducts);
|
Output
Matching products: [
{ name: 'Laptop', price: 999 },
{ name: 'Smartphone', price: 599 },
{ name: 'Tablet', price: 299 }
]
Share your thoughts in the comments
Please Login to comment...