简体   繁体   English

删除 object 后,使数组中的 object 中的每个项目更新

[英]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):所以我最近做了一个 discord 机器人,我做了一个警告系统,所以基本上,我有我的对象(这是在 mongodb 顺便说一句):

{
_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所以数组更长但我想保持简短,所以基本上每次删除数组中的 object 时,我希望更新每个pos ,例如,我删除

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

and i want the second object我想要第二个 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: pos 要更新为 1,现在这似乎很容易,但我想对所有对象执行此操作,但可以说我现在有这样的数组:

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假设我突然删除了 pos 为3的 object,然后我想要更新 4 和 5(4 变为 3,5 变为 4 等等),我知道如何更新对象但想不出方法现在是凌晨 5 点,我很累,所以我希望有人能提出一个方法

Well, since you only want to know how to update values in an array, here it is:好吧,既然你只想知道如何更新数组中的值,这里是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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