
- 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
Add object to array in JavaScript if name does not already exist?
For this, use push() along with forEach(). Following is the code −
Example
var details = [{name:"John"},{name:"David"}] var addObject = ["Mike","Sam"]; addObject.forEach( obj1 => { if(!details.find( obj2 => obj2===obj1 )) details.push({name:obj1}) }) console.log(details);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo165.js.
Output
This will produce the following output −
PS C:\Users\Amit\javascript-code> node demo165.js [ { name: 'John' }, { name: 'David' }, { name: 'Mike' }, { name: 'Sam' } ]
- Related Articles
- Create view in MySQL only if it does not already exist?
- How to check if a table exists in MySQL and create if it does not already exist?
- How Check if object value exists not add a new object to array using JavaScript ?
- PHP and MYSQL database connection and table creation only once if it does not already exist?
- Create a table if it does not already exist and insert a record in the same query with MySQL
- Find pairs in array whose sums already exist in array in C++
- MySQL create user if it does not exist?
- How to check whether a key exist in JavaScript object or not?
- Insert records in MongoDB collection if it does not exist?
- How to create a folder if it does not exist in C#?
- MongoDB query to determine if a specific value does not exist?
- Does NOT EQUAL exist in MySQL?
- Does using SERIAL as column name already includes 'NOT NULL' in MySQL?
- How to check whether a particular key exist in javascript object or array?
- Replacing array of object property name in JavaScript

Advertisements