简体   繁体   中英

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. 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 ),

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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