Repeating a function to remove a Key in an object .forEach()

I want to repeat removePassword() for each array but its not working for some reason?


function deleteManyPasswords (users) 
{
  
 users.forEach(removePassword)


  
  
  
 function removePassword(x)
  {  

    delete x.password;

 


  }

 return users

 
  
}


deleteManyPasswords(array1)


let array1=[{ name: 'foo', password: 'cat' },{ name: 'foo', password: 'cat' },{ name: 'foo', password: 'cat' }]


You are using array1 before you define it.

Unlike var, let and const are not hoisted and can’t be used before they’re defined.

I see you have a habit of using functions and vars before defining them. But this is against best practices. I suggest you always define your variables and functions before you plan on using them.

So…

// not
users.forEach(removePassword)

function removePassword() {}

// but this instead
function removePassword() {}

users.forEach(removePassword)

This will help you avoid any edge case bugs tied to function and variable hoisting.

I got the last one to work but something similar here
saying

TypeError: Cannot read property 'forEach' of undefined
    at countTheObjects:15:7
    at eval:26:1
    at eval
    at new Promise
   





function countTheObjects (arr)
{
  var object1 = 0;

  function numberOfObject (x)
  {
    if(typeof x === Object)
    {
      object1++
    }
    

  }
  arr.forEach(numberOfObject)
  console.log(object1)
 
}

The code looks right. How are you calling countTheObjects? This error implies that you are not passing in an argument to that function.

1 Like

ahhh I got it to run now, outside of the function I was calling the function before I had defined the array1

countTheObjects(array1)

var array1=[{ name: 'foo', password: 'cat' },{ name: 'foo', password: 'cat' },{ name: 'foo', password: 'cat' }]

so I switched it round now so its running fully

still isnt doing what i expected it to though, I want it to add one to object1 for each object in the array ? :l

Because typeof doesn’t return what you think it does

from mdn

The typeof operator returns a string indicating the type of the unevaluated operand.

typeof - JavaScript | MDN

So it returns a string, and you’re checking it against the Object global object constructor.

Also, look into how many results return ‘object’. In javascript everything except primitives are objects. Including arrays.

Read this article for more insight

1 Like

Ill take a look now, its literally the last function I need to make to finish this section of work Im doing for course

done 84 today and yesterday so its gonna bug me until i finish now lol

image

The answer is in the link, but I’ll screenshot it here

1 Like

What am I missing here?


function countTheObjects (x)
{
  var item1 = 0;
  function addToObjects(value)
  {
 
    if( typeof value === 'object' )
    {
      item1++
     } 
 
  }
  x.forEach(addToObjects)
  return item1
}

image

How do I distinguish between lists and other objects
console.log(typeof [])
returns ‘object’
I need a function that distinguishes between lists and null and other objects

Ok so this works for lists but not for null


function countTheObjects (x)
{
  var item1 = 0;
  function addToObjects(value)
  {
 
    if( typeof value === 'object' && value.constructor === Object)
    {
      item1++
     } 
 
  }
  x.forEach(addToObjects)
  return item1
}


For null it returns

image

This is because null is an object as well. So you have to check for that explicitly.


got it to work by removing all the nulls lol


function countTheObjects (x)
{
  var item1 = 0;
  var arr1 = []

  function removeNull(item)
 {
   if(item!==null)

   arr1.push(item)
  }
 x.forEach(removeNull)
  function addToObjects(value)
  {

 
    if( typeof value === 'object' && value.constructor === Object)
    {
      item1++
    } 
    
 
  }
  arr1.forEach(addToObjects)
  return item1
}

That’s one way. But now you’re doing an extra iteration just to remove null. You could just do

if( value !== null && typeof value === 'object' && value.constructor === Object)

Good job figuring it out.