rebote6289
rebote6289

Reputation: 23

Node.js - How to retrieve the value from an array?

var date = {

"1/2/20":500,
"2/2/20":601,
"3/2/20":702,
"4/2/20":803,
"5/2/20":904

}

How do I get only the number so it prints without the date like so:

500
601
702
803
904

Upvotes: 2

Views: 50

Answers (2)

Mrunall Veer
Mrunall Veer

Reputation: 111

You can try the following code:

let date = {

"1/2/20":500,
"2/2/20":601,
"3/2/20":702,
"4/2/20":803,
"5/2/20":904

}

for (let key of Object.keys(date)) {
    let val = date[key];
    console.log(val) 
}

Upvotes: 0

Andy Gaskell
Andy Gaskell

Reputation: 31761

You're looking for Object.values.

var date = {

  "1/2/20":500,
  "2/2/20":601,
  "3/2/20":702,
  "4/2/20":803,
  "5/2/20":904

}

console.log(Object.values(date))
➜  ~ node foo.js
[ 500, 601, 702, 803, 904 ]

If you want to match output exactly add a call to join.

console.log(Object.values(date).join('\n'))
➜  ~ node foo.js
500
601
702
803
904

Upvotes: 5

Related Questions