Append element to an existing list in Node.js

Viewed 27

I know this question will seem to be a very common problem, but trust me I've spent hours searching on Google before asking this question...

I have some data looking like this :

- book1
----- book1 number of pages
----- book1 number of chapters
----- book1 author
- book2
----- book2 number of pages
----- book2 number of chapters
----- book2 author

First question : how should I do ?

I've thought about :

let books_collection = {
 "book 1":
    {
      "number_of_page": 100,
      "number_of_chapters": 5,
      "author": "Author 1"
    },

 "book 2":
    {
      "number_of_page": 200,
      "number_of_chapters": 7,
      "author": "Author 2"
    }
}

Does it looks good to you ? I will need to search easily into those data and save it to a Json file

 

Second question : how can I add a new book to this list?

When I search on Google, I find a lot of tutorials on how to add element to an ARRAY, but not to a list like that...

How to add a new "book 3" to this list ?

 

Third question : how can I add a new subcategory to a book in this list?

If I want to create a new subcategory, like "original_title" , to an existing book, how to do that ?

Thanks a LOT guys :)

1 Answers
let book3 = {
  "book 3": {
    /*properties*/
  }
}

Object.assign(books_collection, book3)

how to add new subcategories: call Object.assign for every book in a loop

let original_title = {
  "original_title": "something"
}

for (let book of Object.values(books_collection) {
  Object.assign(book, original_title)
}
Related