I am accessing a JSON file with nodejs api request.
the file file has 1000's of data.
var data = fileData[0].data has the "date" values in multiple objects. The objects have two values "date" and "value". I just want to print the "date" values of the whole data variable.
"idNumber": "98745973459479574935794577345",
"data": [
{
"date": "2001-09-12T08:37:09.009Z",
"value": 307479
},
{
"date": "2001-09-12T08:36:53.919Z",
"value": 307478
},
{
"date": "2001-09-12T08:36:38.809Z",
"value": 307477
},
There are thousands of these objects within the array. I just want to print all the "date". I tried console.log(data[0]) but it would print the first value and it will also include the "value" which I don't want.
is it possible to print every index with the keyword of "date"? If so, how?
data.forEach(item=>{
console.log(item.date)
})
You need to iterate whole array in order to get the date value.
You can use for loop, while loop, foreach etc, to iterate.
For Each Loop:
let data = [
{
"date": "2001-09-12T08:37:09.009Z",
"value": 307479
},
{
"date": "2001-09-12T08:36:53.919Z",
"value": 307478
},
{
"date": "2001-09-12T08:36:38.809Z",
"value": 307477
},
]
data.forEach(i => {
console.log(i.date)
})
For Loop:
let data = [
{
"date": "2001-09-12T08:37:09.009Z",
"value": 307479
},
{
"date": "2001-09-12T08:36:53.919Z",
"value": 307478
},
{
"date": "2001-09-12T08:36:38.809Z",
"value": 307477
},
]
for(let i = 0; i < data.length; i++){
console.log(data[i].date)
}
Map seems to be a good method for you
NOTE I name the outer object obj so to get the data I use obj.data
const obj = {
"idNumber": "98745973459479574935794577345",
"data": [{
"date": "2001-09-12T08:37:09.009Z",
"value": 307479
},
{
"date": "2001-09-12T08:36:53.919Z",
"value": 307478
},
{
"date": "2001-09-12T08:36:38.809Z",
"value": 307477
}
]
}
console.log(obj.data.map(({date}) => date).join(','))