admin管理员组文章数量:1403435
I am having difficulty deleting key-value pairs on each user object
I have been using the delete method to try to delete the password object as so
return delete Object.keys(users.password)
function deleteManyPasswords(users) {
/*
This function take an array of user objects and deletes the password key value pair on each user object.
E.g.
[
{name: 'Barry', password: 'ilovetea'},
{name: 'Sandeep', password: 'ilovecoffee'},
{name: 'Kavita', password: 'ilovepie'}
]
Returns:
[
{name: 'Barry' },
{name: 'Sandeep'},
{name: 'Kavita'}
]
*/
I am having difficulty deleting key-value pairs on each user object
I have been using the delete method to try to delete the password object as so
return delete Object.keys(users.password)
function deleteManyPasswords(users) {
/*
This function take an array of user objects and deletes the password key value pair on each user object.
E.g.
[
{name: 'Barry', password: 'ilovetea'},
{name: 'Sandeep', password: 'ilovecoffee'},
{name: 'Kavita', password: 'ilovepie'}
]
Returns:
[
{name: 'Barry' },
{name: 'Sandeep'},
{name: 'Kavita'}
]
*/
Share
Improve this question
edited Feb 17, 2020 at 19:29
Brad Larson
170k45 gold badges400 silver badges572 bronze badges
asked May 15, 2019 at 12:48
RowandinhoRowandinho
2037 silver badges16 bronze badges
2
-
users.forEach(u=>delete u.password)
– Niet the Dark Absol Commented May 15, 2019 at 12:51 - If you are doing this to prevent passwords from reaching the user there is probably a better way in the context of what you are doing. – SpeedOfRound Commented May 15, 2019 at 12:53
3 Answers
Reset to default 5You can use map()
with destructuring. Destructure the properties you want to remove and return the rest properties from map()
const arr = [ {name: 'Barry', password: 'ilovetea'}, {name: 'Sandeep', password: 'ilovecoffee'}, {name: 'Kavita', password: 'ilovepie'} ]
function deletePass(arr){
return arr.map(({password,...rest}) => rest)
}
console.log(deletePass(arr))
The above method doesn't work for dynamic properties because you can't name all the properties. For that you can use the following way]
- Create a function which takes two function.
- An array of object
- An array which contain keys which should be removed.
- Use the
map()
function on the array. - Get the entries of each object using
Object.entries()
- Use
filter()
on entries and check if key is not present in the keys to be removed then remove it - Use
Object.fromEntries()
on the filtered entries and you will get the result array of objects.
const arr = [ {name: 'Barry', password: 'ilovetea'}, {name: 'Sandeep', password: 'ilovecoffee'}, {name: 'Kavita', password: 'ilovepie'} ]
function deleteProps(arr,keys){
return arr.map(x =>
Object.fromEntries(
Object.entries(x)
.filter(([k]) => !keys.includes(k))
)
)
}
console.log(deleteProps(arr,["password"]))
You could remove an attribute by using delete
Key.
Have a look below.
var data =[
{name: 'Barry', password: 'ilovetea'},
{name: 'Sandeep', password: 'ilovecoffee'},
{name: 'Kavita', password: 'ilovepie'}
]
function removeKey(items, key){
items.forEach(item=> {
delete item[key]; // remove the attr eg Password
});
return items;
}
console.log(removeKey(data, "password"))
you can use the Array.map
function and then call delete
on each individual entry
function deleteManyPasswords(users) {
return users.map((user) => {
delete user.password;
return user;
});
}
本文标签: javascriptDeleting a key value pair on each user objectStack Overflow
版权声明:本文标题:javascript - Deleting a key value pair on each user object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744370254a2602985.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论