js for循环删除指定下标对象

0: {amount: 1, giftMoney: 0,  price: 12, money: 12, …}
1: {amount: 1, giftMoney: 0,  price: 100, money: 100, …}
2: {amount: 1, giftMoney: 0, price: 100, money: 100, …}

假设我要删除第1个,并且拿到删除后的对象,那应该怎么操作呢

在这里先先谢谢各位大哥大姐的解答了,感谢!

完整的是这样的:

var obj =
[{amount: 1, giftMoney: 0, price: 12, money: 12}
{amount: 1, giftMoney: 0, price: 100, money: 100}
{amount: 1, giftMoney: 0, price: 100, money: 100}]

阅读 3k
3 个回答

是这样吗?

var array = new Array();
var obj1 = {amount: 1, giftMoney: 0, price: 12, money: 12};
var obj2 = {amount: 1, giftMoney: 0,  price: 100, money: 100};
var obj3 = {amount: 1, giftMoney: 0,  price: 200, money: 200};
array.push(obj1);
array.push(obj2);
array.push(obj3);
for (var i = 0;i < array.length; i++){
   if (i == 1){
       array.splice(i,1);
   }
}
console.log(array);

新的代码:

var obj = [{amount: 1, giftMoney: 0, price: 12, money: 12},
{amount: 1, giftMoney: 0, price: 100, money: 100},
{amount: 1, giftMoney: 0, price: 200, money: 200},]
for (var i = 0;i < obj.length; i++){
   if (i == 1){
       obj.splice(i,1);
   }
}
console.log(obj);

运行结果:

> Array [Object { amount: 1, giftMoney: 0, price: 12, money: 12 }, Object { amount: 1, giftMoney: 0, price: 200, money: 200 }]

let arr = arr.splice(0, 1) // 获取被删除返回后的数组aar
let data = arr.splice(0, 1)[0]// 获取被删除返回后数组第0个成员数据

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏