
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to add a new object into a JavaScript array after map and check condition?
For this, you can use filter() along with map().
Example
const details =[ { customerName: 'John', customerCountryName: 'UK', isMarried :true }, { customerName: 'David', customerCountryName: 'AUS', isMarried :false }, { customerName: 'Mike', customerCountryName: 'US', isMarried :false } ] let tempObject = details.filter(obj=> obj.isMarried == true); tempObject["customerNameWithIsMarriedFalse"] = details.filter(obj => obj.isMarried== false).map(obj => obj.customerName); console.log(tempObject);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo176.js.
Output
This will produce the following output −
PS C:\Users\Amit\javascript-code> node demo176.js [ { customerName: 'John', customerCountryName: 'UK', isMarried: true }, customerNameWithIsMarriedFalse: [ 'David', 'Mike' ] ]
- Related Articles
- How Check if object value exists not add a new object to array using JavaScript ?
- map() array of object titles into a new array based on other property value JavaScript
- How to add two arrays into a new array in JavaScript?
- How to convert a plain object into ES6 Map using JavaScript?
- How to turn a JSON object into a JavaScript array in JavaScript ?
- Merge arrays into a new object array in Java
- Convert object to a Map - JavaScript
- How to convert array into array of objects using map() and reduce() in JavaScript
- Formatting JavaScript Object to new Array
- How to merge objects into a single object array with JavaScript?
- How to add new value to an existing array in JavaScript?
- How to add a method to a JavaScript object?
- How to check whether a particular key exist in javascript object or array?
- Joining a JavaScript array with a condition?
- Flat a JavaScript array of objects into an object

Advertisements