admin管理员组文章数量:1021866
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
5 Answers
Reset to default 7Almost.
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
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
5 Answers
Reset to default 7Almost.
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
本文标签: How do I remove an row from an array based on specific condition in JavascriptStack Overflow
版权声明:本文标题:How do I remove an row from an array based on specific condition in Javascript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745496045a2153171.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论