delete object [element]不会删除,而是将其设置为null ....如何删除而不是使其变为null?

时间:2020-03-06 08:04:24

标签: javascript node.js json

Object.keys(tours).forEach(el => {
    if (tours[el].id == 0) ***delete tours[el];***

  });

我想删除“旅行对象”中的“对象”,但是此功能将其设置为null而不是将其删除。

删除操作之前的旅程是这样的

[
  {
    "id": 0,
    "name": "The Forest Hiker",
    "duration": 5,
    "maxGroupSize": 25,
    "difficulty": "easy",
    "ratingsAverage": 4.7
},
  {
    "id": 1,
    "name": "The Sea Explorer",
    "duration": 7,
    "maxGroupSize": 15,
    "difficulty": "medium",
    "ratingsAverage": 4.8
  }
]

,删除后即成为

[
 null,

  {
    "id": 1,
    "name": "The Sea Explorer",
    "duration": 7,
    "maxGroupSize": 15,
    "difficulty": "medium",
    "ratingsAverage": 4.8,
    "ratingsQuantity": 23,
    "price": 497,
    "summary": "Exploring the jaw-dropping US east coast by foot and by boat",
    "description": "Cpariatur.",
    "imageCover": "tour-2-cover.jpg",
    "images": ["tour-2-1.jpg", "tour-2-2.jpg", "tour-2-3.jpg"],
    "startDates": ["2021-06-19,10:00", "2021-07-20,10:00", "2021-08-18,10:00"]
  }
]

5 个答案:

答案 0 :(得分:1)

您不应使用delete从数组中删除元素。

您可以使用splice从数组中删除元素。

tours.forEach( (el, index) => {
 if (tours[el].id == 0) tours.splice(index, 1)
});

如果该条件对于数组中的多个元素为true,则需要考虑到您在对其进行迭代时正在对数组进行变异,因此需要考虑删除的元素数。

deletedCount = 0

Object.keys(tours).forEach( (el, index) => {
  if(tours[el-deletedCount].id == 2 ) { 
   tours.splice(index-deletedCount, 1); 
   deletedCount++ 
  }
});

答案 1 :(得分:1)

我看到tours只是对象数组。所以你可以这样做:

以任何条件删除

tours = tours.filter(t => t.id !== 0);

删除给定的索引n

tours.splice(n, 1)

专门删除索引0

tours.shift()

答案 2 :(得分:0)

您应该使用Array#splice

删除多个项目

如果数组可以包含要删除的多个值,则可以使用Array#reduceRight之类的函数从头到尾迭代整个数组(如果走另一个数组并删除索引为有些项目会发生变化。

var tours = [{
  "id": 0,
  "name": "The Forest Hiker",
  "duration": 5,
  "maxGroupSize": 25,
  "difficulty": "easy",
  "ratingsAverage": 4.7
}, {
  "id": 1,
  "name": "The Sea Explorer",
  "duration": 7,
  "maxGroupSize": 15,
  "difficulty": "medium",
  "ratingsAverage": 4.8
}];

tours.reduceRight(function(res, item, index) {
  if (item.id === 0) tours.splice(index, 1);
}, 0);

console.log(tours);

删除0-1个项目

如果带有id === 0的项目只能出现0到1次,那么您可以使用:

var tours = [{
  "id": 0,
  "name": "The Forest Hiker",
  "duration": 5,
  "maxGroupSize": 25,
  "difficulty": "easy",
  "ratingsAverage": 4.7
}, {
  "id": 1,
  "name": "The Sea Explorer",
  "duration": 7,
  "maxGroupSize": 15,
  "difficulty": "medium",
  "ratingsAverage": 4.8
}];

var index = tours.findIndex(function(item) {
  return item.id === 0;
});
if (index !== -1) tours.splice(index, 1);

console.log(tours);

使用正确的项目创建新数组

最后,如果您不关心更改数组引用,可以只使用Array#filter

var tours = [{
  "id": 0,
  "name": "The Forest Hiker",
  "duration": 5,
  "maxGroupSize": 25,
  "difficulty": "easy",
  "ratingsAverage": 4.7
}, {
  "id": 1,
  "name": "The Sea Explorer",
  "duration": 7,
  "maxGroupSize": 15,
  "difficulty": "medium",
  "ratingsAverage": 4.8
}];

tours = tours.filter(function(item) {
  return item.id !== 0;
});

console.log(tours);

答案 3 :(得分:0)

您正尝试像使用array那样对object进行迭代,可以使用Array.filter获得一个经过过滤的数组,而不会使原始数组发生变化。

filteredTours = tours.filter(tour => tour.id == 0)

您也可以使用拼接来完成此操作

tours.forEach((tour,index)=> if(tour.id===1){ tours.splice(index,1) }

答案 4 :(得分:0)

Object.keys()用于迭代Object的道具。在这里,您最好直接使用for循环或forEach。当找到匹配的对象时,只需将其拼接即可。有帮助吗?

for (let i = 0; i < tours.length; i++) {
  if (tours[i].id === 0) {
    return tours.splice(i, 1);
  }
}