0

Excuse my english, im from argentina. I have an object in JS called listaTurnos (obtained from a ajax call)

var listaTurnos = []
$(document).ready(function() {
    $.ajax({
        type: 'POST',
        url: 'ajax/turnos2.ajax.php',
        dataType: "json",
        data: { item: "a" },
        success: function(data) {
            data.forEach(e => {
                listaTurnos.push(e["datos"])
            });
        },
        error: function(data) {
        }
    });
});
console.log(listaTurnos)

This is how it looks in the console:

[]
0: "{"title":"Agustin Guerra","start":"2020-03-12T10:30:00","end":"2020-03-12T11:30:00"}
↵"
1: "{"title":"Mariel Guerrieri","start":"2020-03-15T09:30:00","end":"2020-03-15T10:30:00"}
↵"
length: 2
__proto__: Array(0)

I don't know how get the values {"title":"Agustin Guerra","start":"2020-03-12T10:30:00","end":"2020-03-12T11:30:00"} for example.

I try with for..in, with foreach and for, without success.

listaTurnos.forEach(e => {
    console.log(e.title)
        // etc
});

Returns nothing

console.log(JSON.stringify(listaTurnos))

Returns []

for (let i = 0; i < listaTurnos.length; i++) {
    console.log(listaTurnos[i])
}

Returns nothing

I'm really lost

my goal is, from the object, to get the following structure

[
{
    title: 'Agustin Guerra',
    start: '2020-03-12T10:30:00',
    end: '2020-03-12T11:30:00'
},
{
    title: 'Mariel Guerrieri',
    start: '2020-03-15T09:30:00',
    end: '2020-03-15T10:30:00'
}

]
Agustin G.
  • 72
  • 3
  • 19
  • 1
    "to get the following string" - that's not a string ... `console.log(JSON.stringify(listaTurnos)) Returns []` ... chances are you're trying to access `listaTurnos` before the AJAX call is run - please show the code where `listaTurnos` is received and then accessed by the code you've tried – Jaromanda X Mar 16 '20 at 03:34
  • @JaromandaX I just edited my question – Agustin G. Mar 16 '20 at 03:38
  • i'm not sure that i understood but listaTurnos[0] should return you your values. keep me updated pls – Antoine553 Mar 16 '20 at 03:38
  • @Antoine553 I thought the same, but it returns ``undefined`` – Agustin G. Mar 16 '20 at 03:39
  • `listaTurnos` is populated asynchronously at some unknown time in the future, `console.log(listaTurnos)` happens BEFORE the request is even sent. The console appears to show `listaTurnos` as having data, but the console evaluates the output at the time you click on it, so it's not an actual representation of the data at the time of the console.log ... if you instead do `console.log(listaTurnos.length)` you WILL get zero there – Jaromanda X Mar 16 '20 at 03:40
  • You're right, ``console.log(listaTurnos.lenght)`` returns 0, but ``console.log(listaTurnos)`` return all the data. it´s weird – Agustin G. Mar 16 '20 at 03:44

0 Answers0