3

I have an array of objects like this one.

var books = [{
id : 1,
name : 'Name of the wind',
year : 2015,
rating : 4.5,
author : 2}];

Now I have a function editBooks which asks user for an id and replaces the book with same id with the values given by the user. For example

function editBooks(name,author,year,rating,id)

How can i replace the contents of objects inside my books array based on id provided by the user?

Shehzadi khushi
  • 275
  • 1
  • 4
  • 20

5 Answers5

7

You could serach for the id and use the book for update. If no book is found, generate a new entry.

function editBooks(name, author, year, rating, id) {
    var book = books.find(b => b.id === id);
    if (book) {
        book.name = name;
        book.author = author,
        book.year = year;
        book.rating = rating;
    } else {
        books.push({ id, name, author, year, rating });
    }
}

var books = [{ id: 1, name: 'Name of the wind', year: 2015, rating: 4.5, author: 2 }];

editBooks('Foo', 2017, 3.3, 5, 1);
editBooks('bar', 2016, 1, 2, 2);
console.log(books);

For a slightly better implementation, i would move id to the first place of the parameters and use a check for all parameters to update only the ones who are not undefined, because aof a possible update of only one property.

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • This is the correct answer. It doesn't introduce unnecessary loops that obfuscate the simple logic. – Martin May 28 '17 at 12:30
1

You could pass object as parameter to your function and use for...in loop to update object with same id if found.

var books = [{id: 1,name: 'Name of the wind',year: 2015,rating: 4.5,author: 2}];

function editBooks(obj) {
  books.forEach(function(e) {
    if(obj.id && obj.id == e.id) {
      for(var i in obj) e[i] = obj[i]
    }
  })
}

editBooks({id:1, name: 'New name', author: 22})
console.log(books)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
1

It's best to pass an object that contains only the changes (an object with only the property or properties to change their values)

In general you may simply do as follows;

var books = [{id : 1, name : 'Name of the wind', year : 2015, rating : 4.5, author : 2}, {id : 2, name : 'River Guard', year : 2016, rating : 6.5, author : "John Doe"}];
Object.assign(books.find(b => b.id === 2),{author: "Jane Doe"});
console.log(books);

Turning into a function like

function editBook(bookList, id, edits){
  Object.assign(bookList.find(b => b.id === id),edits);
  return bookList;
}

is trivial.

Redu
  • 25,060
  • 6
  • 56
  • 76
0

Try below snippet,

function editBooks(name,author,year,rating,id) {
    var found = false;
    books.forEach(function(book) {
        if(book.id == id) {
           book.name = name;
           book.year = year ;
           book.author = author;
           book.rating = rating;
           found = true;
        }
   });
   return found; // if found is false, then you can insert new book
}
Ajay
  • 2,483
  • 2
  • 16
  • 27
0
  1. Only changed values are updated.

  2. New Books will be added if their id's do not exist, and if the new book has undefined values those values are set as undefined.

var books = [
  {
id : 1,
name : 'Name of the wind',
year : 2015,
rating : 4.5,
author : 2},
{id:2,
name : 'Name of the 2',
year : 2015,
rating : 4.5,
author : 2},
{id:3,
name : 'Name of the 3',
year : 2015,
rating : 4.5,
author : 2}
];

function editBooks(books,id,name,year,rating,author){
const update = book => Object.assign(book, { id:book.id,name: name || book.name,year: year || book.year,rating: rating || book.rating,author: author || book.author});
let details = books.find((book)=>{ return book.id===id });
details ? update(details):books.push({id,name ,year,rating, author});
};

//edit if exists
editBooks(books,2,'Updated Title','1985');
// add if id does not exist
editBooks(books,4,'A Whole New Title', 2015 );
console.log(books)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Rick
  • 1,035
  • 10
  • 18