![](/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.