0

I have an JSON file which contains a property named dateOfBirth and most of objects have different format (for example: one has 1.9.2012, and the other one has 02.04.1929). I wrote a function that changes the dates format to be the same in every single object, but I fail at connecting it to the array. What am I doing wrong? How can it be fixed? It has to be done in pure Javascript, without jQuery or any frameworks.

const endpoint = 'https://api.myjson.com/bins/agowj';
const people = [];
fetch(endpoint)
    .then(blob => blob.json())
    .then(data => people.push(...data));


people.dateOfBirth.forEach(function(elem) {

    var newtab = [];
    elem.split("-").forEach(function(elemi) {
        if (elemi.length === 1) {
            newtab.push("0" + elemi);
        } else {
            newtab.push(elemi);
        }
    })
    console.log(newtab.join("-"));
})
Tomasz Czechowski
  • 255
  • 1
  • 2
  • 12
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) Simply replace "return" with "pushing to external array" – Yury Tarabanko Apr 27 '17 at 15:13
  • Can you tell us what's the format of the objects contained in the `people` array ? – Erazihel Apr 27 '17 at 15:14
  • You are trying to read your data, **before it comes back from the call**. By the way: is your JSON something like: `{"dateOfBirth": ["1.9.2012", "02.04.1929"]}` or `[{"dateOfBirth": "1.9.2012"}, ...]` ? – Vicenç Gascó Apr 27 '17 at 15:14
  • @VicençGascó [{"dateOfBirth": "1.9.2012"}, ...]. (second option in your comment) – Tomasz Czechowski Apr 27 '17 at 15:30
  • What do you mean by "but I fail at connecting it to the array."? – Pankaj Shukla Apr 27 '17 at 15:38
  • @PankajShukla I tested it on normal array (`var test = ["20-02-2012", "1.2.2010"]` for example) and it worked. But now I have to use a JSON file with `[{"dateOfBirth": "1.9.2012"}, ...]` instead of test and I don't know how to do that. – Tomasz Czechowski Apr 27 '17 at 15:45
  • @TomaszCzechowski then, to get to your information you need to `people.forEach(function(elem) { var dateOfBirth = elem.dateOfBirth; /* apply here your logic */ });` – Vicenç Gascó Apr 28 '17 at 10:51

1 Answers1

0

You need to parse people.dateOfBirth in success callback of your api call. Now your code is executing line by line and when it comes to people.dateOfBirth.forEach your people variable is still an empty array.

    const endpoint = 'https://api.myjson.com/bins/agowj';
    const people = [];
    fetch(endpoint)
        .then(blob => blob.json())
        .then((data) => {
            people.push(...data);
            people.forEach((person) => {
              var newtab = [];
              person.dateOfBirth.split("-").forEach(function(elemi) {
                if (elemi.length === 1) {
                    newtab.push("0" + elemi);
                } else {
                    newtab.push(elemi);
                }
                 console.log(newtab.join("-"));
              })
             })
         });


    

<!-- end snippet -
<!-- begin snippet: js hide: false console: true babel: false -->
Maciej Szpyra
  • 313
  • 1
  • 12