0

I have a JSON file that returns this :

{
  "id": 1,
  "name": "",
  "lastName": "",
  "birthDate": "",
  "invoice": [
    {
      "id": 1,
      "date": "20/10/2015"
    },
    {
      "id": 2,
      "date": "20/8/2013"
    },
    {
      "id": 3,
      "date": "20/6/2012"
    }
  ]
}

if I access invoice[0].date I will get "20/10/2015".

I need to save in an array all three dates in this case, so that I can show it in a dropdown. <select> option should have all three dates.

How do I save and access those in JavaScript?

var allDates=data.info[all arrays].date // date field of every object
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
MonnIca
  • 81
  • 2
  • 8

1 Answers1

2

I'd use Array.map() for this - it executes a function against every element of an array and makes a new array from the results. So you can do this:

function toDate( item ) {
   return item.date;
}
var allDates = info.map( toDate );
Duncan Thacker
  • 5,073
  • 1
  • 10
  • 20