1
var obj = {a: 1, b: 2, c:3}    

Object.keys(obj).forEach(function(x){
    console.log(obj[x])
})

This gives: 1 2 3

so how can I make it to work give me 1 4 9 (e.g. times by itself) I thought this would work

Object.keys(obj).forEach(function(x){
    console.log(obj[x*x])
})
BenG
  • 14,826
  • 5
  • 45
  • 60
The worm
  • 5,580
  • 14
  • 36
  • 49

4 Answers4

3

you need to multiply the values.

you have x*x which would be 'a'*'a' results in NaN.

obj[NaN] = undefined

var obj = {a: 1, b: 2, c:3}

Object.keys(obj).forEach(function(x){
    console.log(obj[x] * obj[x])
})
BenG
  • 14,826
  • 5
  • 45
  • 60
0

You're almost there. this one does the job:

Object.keys(obj).forEach(function(x){
    console.log(obj[x]*obj[x])
 })
Alireza Eliaderani
  • 1,974
  • 16
  • 13
0

you can use Object.values in ES2016

Object.values(obj).forEach(function(x){
   console.log(x*x);
})
Darshan
  • 1,064
  • 7
  • 15
0

var obj = {a: 1, b: 2, c:3}

Object.keys(obj).forEach(function(x){
    console.log(Math.pow(obj[x], 2));
})

Math.pow (x,y) => xxx*...y times...*x

LellisMoon
  • 4,810
  • 2
  • 12
  • 24