![](/img/trans.png)
[英]How to create a Set from Array and remove original items in JavaScript
[英]How to remove items from array in javaScript
我需要删除数组中的特定项目。下面是数组(状态)
0: {id: 5, description: "Assign"}
1: {id: 8, description: "Business Case Review"}
2: {id: 4, description: "Cancelled"}
3: {id: 3, description: "Closed"}
4: {id: 6, description: "Confirm"}
5: {id: 7, description: "Not Started"}
6: {id: 2, description: "On Hold"}
7: {id: 18, description: "Pending CEMT Approval"}
8: {id: 9, description: "Problem Statement Review"}
9: {id: 10, description: "Review Board"}
Code:
Status.forEach(function (ap)
{if ( ap.description != 'Not Started'
&& ap.description !='Review Board'
&& ap.description != 'Cancelled'
&& ap.description !='Business Case Review'
&& ap.description !='Problem Statement Review'
)
{
Status.splice(Status.indexOf(ap), 1);
}
})
输出:
业务案例审查、取消、确认、未开始、等待 CEMT 批准、问题陈述审查、审查委员会。 期望输出:业务案例审查、取消、未开始、问题陈述审查、审查委员会。
在检查删除项目下方数组中的项目后的条件是否会移动到上一个项目索引时,因为当前项目不会来进行验证,这是导致问题的原因。 有人可以在这方面提供帮助。
您可以使用过滤器方法( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter ),
例子 :
array.filter(element => element.description !== "Business Case Review")
将返回一个包含所有对象的新数组,除了以“Business Case Review”作为描述的对象
将您的过滤器定义为一个数组,然后使用该过滤器过滤您的数组,如下所示:
const filters = [
'Not Started',
'Review Board',
'Cancelled',
'Business Case Review',
'Problem Statement Review'
]
var filteredResult = yourArray.filter(element => !filters.includes(element.description))
let Status = [
{id: 5, description: "Assign"},
{id: 8, description: "Business Case Review"},
{id: 4, description: "Cancelled"},
{id: 3, description: "Closed"},
{id: 6, description: "Confirm"},
{id: 7, description: "Not Started"},
{id: 2, description: "On Hold"},
{id: 18, description: "Pending CEMT Approval"},
{id: 9, description: "Problem Statement Review"},
{id: 10, description: "Review Board"}
];
let listOfRemoveItemIndex = []
for (let index = 0; index < Status.length; index++) {
const ap = Status[index];
if ( ap.description != 'Not Started' && ap.description !='Review Board' && ap.description != 'Cancelled' && ap.description !='Business Case Review' && ap.description !='Problem Statement Review')
{
listOfRemoveItemIndex.push(index);
}
}
listOfRemoveItemIndex.forEach((index) => {
Status.splice(index, 1);
});
console.log(" Status : ", Status);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.