

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
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 Questions & Answers
- 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?
- PHP and MYSQL database connection and table creation only once if it does not already exist?
- MySQL create user if it does not exist?
- Insert records in MongoDB collection if it does not exist?
- Create a table if it does not already exist and insert a record in the same query with MySQL
- Does NOT EQUAL exist in MySQL?
- MongoDB query to determine if a specific value does not exist?
- How to create a folder if it does not exist in C#?
- Does using SERIAL as column name already includes 'NOT NULL' in MySQL?
- Java Selenium Chromedriver.exe Does not Exist IllegalStateException
- Find pairs in array whose sums already exist in array in C++
- How can I create a python directory if it does not exist?
- How to check whether a key exist in JavaScript object or not?
- How can I create a directory if it does not exist using Python?
Advertisements