简体   繁体   English

如何从javaScript中的数组中删除项目

[英]How to remove items from array in javaScript

I need to remove specific items in array.Below is the array(Status)我需要删除数组中的特定项目。下面是数组(状态)

    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);
      }
      })

Output :输出

Business Case Review, Cancelled, Confirm, Not Started, Pending CEMT Approval, Problem Statement Review, Review Board.业务案例审查、取消、确认、未开始、等待 CEMT 批准、问题陈述审查、审查委员会。 Desired Output:Business Case Review, Cancelled, Not Started, Problem Statement Review,Review Board.期望输出:业务案例审查、取消、未开始、问题陈述审查、审查委员会。

when checking if condition after deleting item in the array below item will move to previous item index since the current item wont come for validation this is causing the problem.在检查删除项目下方数组中的项目后的条件是否会移动到上一个项目索引时,因为当前项目不会来进行验证,这是导致问题的原因。 Can some one assist in this regards.有人可以在这方面提供帮助。

You can use the filter method ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter ),您可以使用过滤器方法( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter ),

Example :例子 :

array.filter(element => element.description !== "Business Case Review")

will return you a new array with all the objects except the one which has "Business Case Review" as description将返回一个包含所有对象的新数组,除了以“Business Case Review”作为描述的对象

Define your filters as an array and then filter your array with that filter as follows:将您的过滤器定义为一个数组,然后使用该过滤器过滤您的数组,如下所示:

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.

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