0

How to remove a key from an object

This function takes an array of user objects and deletes the moto key-value pair on each user object.

I have tried this code:

function deleteAllMoto(users) {

   for(var i=0; i<=users.length; i++) {

    delete users[i].password;
    return users[i];

}

2 Answers 2

1

Something like this:

function deleteAllMoto (arr) {
  arr.forEach(function(item) {
    delete item.moto
  })
  return arr
}

From your code you are deleting password not moto, then trying to return each user instead of the whole array after the mutations.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, It works. One more thing how to count number of objects in the array?
you can just use length e.g. arr.length
0

deleteAllMoto function should be

function deleteAllMoto(users) {

  for(var i=0; i<=users.length; i++) {
    delete users[i].moto;
  }
  return users;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.