简体   繁体   中英

Making each item in a object in a array update once a object is deleted

So I made a discord bot recently and I made a warning system,so basicly,I have my object(this is in mongodb btw):

{
_id: '0123',
warns: [
 {
  pos: 1,
  reason: 'apples r blue'
 },
 {
  pos: 2,
  reason: 'apples r red'
 }
]}

so the array is longer but i wanna keep it short,so basicly everytime a object in the array is deleted,i want each pos to be updated,for example,i delete

 {
  pos: 1,
  reason: 'apples r blue'
 }

and i want the second object

 {
  pos: 2,
  reason: 'apples r red'
 }

pos's to be updated to 1,now this seems easy but i wanna do this for ALL objects,but lets say i have the array like this now:

warns: [
 {
  pos: 1,
  reason: 'apples r blue'
 },
 {
  pos: 2,
  reason: 'apples r red'
 },
 {
  pos: 3,
  reason: 'apples r red'
 },
 {
  pos: 4,
  reason: 'apples r red'
 },
 {
  pos: 5,
  reason: 'apples r red'
 }
]}

and lets say i suddenly removed the object with a pos of 3 ,then i want 4 and 5 to be updated as well (4 becomes 3 and 5 becomes 4 and so on),i know how to update the objects but cant think of a method as its 5am right now and im tired,so i hope someone will suggest a method

Well, since you only want to know how to update values in an array, here it is:

array.map((value) => {
  value.pos--;
  return value;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM