0

I am currently utilizing the SheetSU API to read data from a Google Sheets file.

var members = Sheetsu.read("URL", {}, successFunc);

For now, the successFunc is a function that takes the input data to console.log() to me.

The data imports like this: (14){{..},{..},{..}....} and each object within this dictionary looks something like this:

0: {Name: "First Last", ID: "12536723", Status: "Member"}
1: {Name: "First Last", ID: "12371238", Status: "Member"}
...
14: {Name: "First Last", ID: "12341228", Status: "Member"}

I need to pull out the values of Name and ID, however:

for (var x in data) {
    console.log(x)
}

= 0, 1, 2, ..., 14 (as ints not objects)

for (var x in data) {
    console.log(x.Name)
}

= undefined, undefined, ..., undefined

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
JoshK
  • 425
  • 1
  • 4
  • 18
  • See the warning and alternatives about using `for...in` with arrays: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in#Array_iteration_and_for...in – Mark Sep 05 '18 at 17:42
  • @Igor Op did not ask about the difference, did not even mention `for of` – Emeeus Sep 05 '18 at 17:50
  • Possible duplicate of [How do I loop through or enumerate a JavaScript object?](https://stackoverflow.com/q/684672/1260204) – Igor Sep 05 '18 at 17:59

1 Answers1

3

x is not a value It is a index like 0,1 ...14, you can use of for that or use data[x] inside in.

var data = [{
        Name: "First Last",
        ID: "12536723",
        Status: "Member"
    }, {
        Name: "First Last",
        ID: "12371238",
        Status: "Member"
    },
    {
        Name: "First Last",
        ID: "12341228",
        Status: "Member"
    }
]

for (var x in data) {

    console.log(data[x].Name); //x is not a value It is a index like 0,1 ...14

}

for (var x of data) {

    console.log(x.Name); // use of which prints x as object not index

}
Nishant Dixit
  • 5,388
  • 5
  • 17
  • 29