How do I remove an row from an array based on specific condition in Javascript

Viewed 111

I'm having an array named userList and get it get filled by pushing some data in to it via a loop... like this

userList.push({
  userProfileID : dataEntry.UserProfileID ,
  isAgent       : dataEntry.isAgent       ,
  firstName     : dataEntry.firstName     ,
  roleNames     : dataEntry.roleNames
})

and the output will be like this and 100 more records

0: Object {userProfileID: "68670", isAgent: false, firstName: "ARSDEO", roleNames:"Deo Role"}
1: Object {userProfileID: "68672", isAgent: false, firstName: "ARSBM101", roleNames:"BM Role"}
2:.......
3:.......

Here I want to remove the entry which has roleNames = 'BM role' in userList array

I tried this :

userList = userList.filter(item => userList.roleNames == 'BM Role');

but I get no record in array. Please advice...

5 Answers

Almost.

You want to check if the property rolesNames on item doesn't equal "BM Role" (ie you want to filter those out)

const newList = userList.filter(item => item.roleNames !== 'BM Role');

Note: filter, like the other functional array methods map and reduce, doesn't mutate the array, it returns a new array with only those elements that match the condition in the callback.

Try to check if roleNames isn't equal to 'Bm Role'.

userList = userList.filter((user) => {
    return user.roleNames !== 'BM role';
})

BTW filter() method creates a copy of original array.

But if you want to delete directly from original array, I suggest to you use splice() method:

for(let i = 0; i < userList.length; i++){ 
   if (userList[i].roleNames === 'BM Role') {
      userList.splice(i, 1); 
      i--;
   }
}

IF the question is about removing elements fron userList:

var userList = 
[ { userProfileID: '68670', isAgent: false, firstName: 'ARSDEO', roleNames: 'Deo Role' } 
, { userProfileID: '68672', isAgent: false, firstName: 'aaaaaa', roleNames: 'aaa Role' } 
, { userProfileID: '68674', isAgent: false, firstName: 'bbbbbb', roleNames: 'BM Role'  } 
, { userProfileID: '68676', isAgent: false, firstName: 'cccccc', roleNames: 'BM Role'  } 
, { userProfileID: '68678', isAgent: false, firstName: 'dddddd', roleNames: 'bbb Role' } 
] 
  

for (let i=userList.length;i--;)  // start from end to zero
{
  if (userList[i].roleNames==='BM Role') { userList.splice(i, 1) }
}

for (let elm of userList ) { console.log( JSON.stringify(elm) )  }

your filter is not correct:

userList = userList.filter(item => item.roleNames != 'BM Role');

the item variable is what you are checking against. On each filter call a new item is passed to the function, and this is what is being evaluated

You should use item.roleNames instead of userList.roleNames, since roleNames is a part of the specific object/item in the array and not the array itself. Additionally you should probably use item.roleNames != 'BM Role', because filter returns an array that contains the matching items, and you want to remove those that match.

Related