D
D
Dmitry Maksimyuk2020-07-07 17:36:27
JavaScript
Dmitry Maksimyuk, 2020-07-07 17:36:27

How to add new item to json in node js?

There is this code:

vk.updates.on('message', async (next, message) => {
  if(message.isUser == 0) return;
  for(i=0; i < users.length;i++){
  if(next.senderId == users[i].id) return message();
  }
  users.push({
    id: next.senderId,
    bot_id: users.length,
    tag: 'Пользователь',
    ban: false,
    mention: true,
    balance: 100000,
    admin_level: 0,
    exp: 0,
    lvl: 0,
    kvest: 0,
    test: 0,
    business_lvl: 0,
    job_level: 1
  });
  saveUsers();
  return message();
});


The code works, but the problem is that if you add a new item to users.push() and if there are users in the JSON file, then they will not add a new item, and since this is a game bot, in case of any updates, you need to add a new item, and not as I'm doing now, I delete the data from json and new ones are created. I read about JSON on learn.javascript.ru, but did not find the answer to my question

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2020-07-07
@Robur

read all users from the file into an array, add a new item to them all ("user.newProp = 2") in a loop, then save back.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question