Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to add a new object into a JavaScript array after map and check condition?
In JavaScript, you can add new objects or properties to an array after filtering and mapping by combining filter(), map(), and array manipulation techniques.
Understanding the Approach
The technique involves first filtering data based on a condition, then adding computed properties or objects derived from mapping operations on the original data.
Example: Adding Property to Filtered Array
const details = [
{ customerName: 'John', customerCountryName: 'UK', isMarried: true },
{ customerName: 'David', customerCountryName: 'AUS', isMarried: false },
{ customerName: 'Mike', customerCountryName: 'US', isMarried: false }
];
// Filter married customers
let tempObject = details.filter(obj => obj.isMarried == true);
// Add unmarried customer names as a new property
tempObject["customerNameWithIsMarriedFalse"] = details
.filter(obj => obj.isMarried == false)
.map(obj => obj.customerName);
console.log(tempObject);
[
{ customerName: 'John', customerCountryName: 'UK', isMarried: true },
customerNameWithIsMarriedFalse: [ 'David', 'Mike' ]
]
Adding Complete Objects After Mapping
You can also add complete objects to the array after processing:
const details = [
{ customerName: 'John', customerCountryName: 'UK', isMarried: true },
{ customerName: 'David', customerCountryName: 'AUS', isMarried: false },
{ customerName: 'Mike', customerCountryName: 'US', isMarried: false }
];
// Get married customers
let result = details.filter(obj => obj.isMarried == true);
// Create summary object from unmarried customers
const unmarriedSummary = {
type: 'summary',
unmarriedCustomers: details
.filter(obj => obj.isMarried == false)
.map(obj => ({ name: obj.customerName, country: obj.customerCountryName }))
};
// Add summary object to result array
result.push(unmarriedSummary);
console.log(result);
[
{ customerName: 'John', customerCountryName: 'UK', isMarried: true },
{
type: 'summary',
unmarriedCustomers: [
{ name: 'David', country: 'AUS' },
{ name: 'Mike', country: 'US' }
]
}
]
Using Spread Operator for Cleaner Approach
const details = [
{ customerName: 'John', customerCountryName: 'UK', isMarried: true },
{ customerName: 'David', customerCountryName: 'AUS', isMarried: false },
{ customerName: 'Mike', customerCountryName: 'US', isMarried: false }
];
const marriedCustomers = details.filter(obj => obj.isMarried);
const processedUnmarried = details
.filter(obj => !obj.isMarried)
.map(obj => ({ ...obj, status: 'processed' }));
const finalResult = [...marriedCustomers, ...processedUnmarried];
console.log(finalResult);
[
{ customerName: 'John', customerCountryName: 'UK', isMarried: true },
{ customerName: 'David', customerCountryName: 'AUS', isMarried: false, status: 'processed' },
{ customerName: 'Mike', customerCountryName: 'US', isMarried: false, status: 'processed' }
]
Conclusion
You can add objects to arrays after filtering and mapping using array methods like push(), spread operator, or direct property assignment. Choose the approach based on whether you need to add properties or complete objects to your result.
Advertisements
