繁体   English   中英

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

[英]Making each item in a object in a array update once a object is deleted

所以我最近做了一个 discord 机器人,我做了一个警告系统,所以基本上,我有我的对象(这是在 mongodb 顺便说一句):

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

所以数组更长但我想保持简短,所以基本上每次删除数组中的 object 时,我希望更新每个pos ,例如,我删除

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

我想要第二个 object

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

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'
 }
]}

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

好吧,既然你只想知道如何更新数组中的值,这里是:

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

暂无
暂无

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

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