![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Assign new value to item in an array if it matches another item without looping in JavaScript?
For this, use filter() along with map(). Let’s say the following is our array −
const studentDetails = [ {Name: "John"}, {Name: "David"}, {Name: "Bob"}, {Name: "Mike"} ]
We will assign a new value to the name “Bob”. Following is the code −
Example
const studentDetails = [ {Name: "John"}, {Name: "David"}, {Name: "Bob"}, {Name: "Mike"} ] var changeName = "Bob"; studentDetails.filter((obj) => obj.Name === changeName).map((obj) => obj.Name = "Carol"); console.log(studentDetails);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo98.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo98.js [ { Name: 'John' }, { Name: 'David' }, { Name: 'Carol' }, { Name: 'Mike' } ]
Advertisements