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
Add a ment  | 

3 Answers 3

Reset to default 5

You 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