最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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

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

Share Improve this question edited Oct 26, 2019 at 13:21 Mister Jojo 22.6k6 gold badges25 silver badges44 bronze badges asked Oct 26, 2019 at 13:14 Mar1009Mar1009 8112 gold badges13 silver badges29 bronze badges 1
  • filter doesn't remove anything, it creates a new array. To remove an element, use splice. – RobG Commented Oct 26, 2019 at 13:21
Add a ment  | 

5 Answers 5

Reset to default 7

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

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.

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

发布评论

评论列表(0)

  1. 暂无评论