-2

I have an object (obj) that contains the content of an excel sheet (using node-xlsx library). console.log(obj) gives me the following data structure:

[ { name: 'Autostar Report',
data:
 [ [Object],
   [],
   [Object],
   [Object],
   [Object],
   [Object],
   [Object],
   [Object],
   [Object],
   [Object],
   [Object],
   [],
   [object]
]}]

Each of the objects in data contain values like this:

[
            8,
            "Enugu - Abuja",
            "Sienna Executive ( 1st )",
            5,
            "9,000",
            "45,000",
            "5,500",
            "39,500"
        ],
        [
            14,
            "Enugu - Lagos",
            "Sienna Executive ( 1st )",
            5,
            "9,000",
            "45,000",
            "8,600",
            "36,400"
        ]

Each of the object contained in data contains 8 values. I want to insert the values into a database table. Each of the value goes to a field in the table. Please how do I access the values in each data object?

Chibuzo
  • 6,112
  • 3
  • 29
  • 51
  • 2
    Be more specific. What's the name of the object? Which value(s) do you need to access? Do you need them for specific objects? Do you want a new object / array created with the value(s) you want from the object(s)? – random_user_name Feb 06 '16 at 15:59
  • Possible duplicate: http://stackoverflow.com/questions/3010840/loop-through-array-in-javascript – Igor Feb 06 '16 at 15:59
  • @cale_b I need to access the data property. I want to insert each row into a database. – Chibuzo Feb 06 '16 at 16:28
  • You answered only one of the many questions I asked: What's the name of the object? If you need the "data" property - that contains many additional properties - what do you need from them? Show us your code for adding to the database. NOTE: To access the data property, assuming the name of the object above is "sheet", you could simply say `var data = sheet.data`. BUT, then what do you do with it? At this point, `data` is an array of objects. You'd want to get to each one, yes? – random_user_name Feb 06 '16 at 16:36
  • This answer has all the bits you need: http://stackoverflow.com/a/3010848/870729 - but we can't glue it up into an answer for you without more details. Your question is unclear still. – random_user_name Feb 06 '16 at 16:37
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Teemu Feb 06 '16 at 17:58
  • @cale_b The name is obj. The objects in data doesn't contain any property, just the values that I need. Each of the value goes to a field in the database table. I don't have issue with inserting into the database. I just can't figure out how to access the values contained in the objects. – Chibuzo Feb 06 '16 at 19:02

1 Answers1

2

Note that you keep saying "object", when really the obj.data value is an array of arrays. With arrays, you access a value by numerical index.

So, given your data structure, and the fact that you have only selected javascript, you would access the data like so:

// ensure the object has a "data" attribute
if (obj.hasOwnProperty('data')) {
  // get the data into a variable for convenience
  var data = obj.data;
  // loop over each row
  for (i = 0; i < data.length; i++) {
    // put the row into a variable for convenience
    var row = data[i];
    // since it's an array, access each value by index
    console.log(row[0]);
    console.log(row[1]);
    console.log(row[2]);
    // ... etc ...
  }
}

Here's a working fiddle

When it comes to processing things like this, a library such as underscore or lodash sure come in handy.

random_user_name
  • 25,694
  • 7
  • 76
  • 115
  • Thanks so much Caleb. obj.data value is really an Array I know, it's just that [object] is shown on the console instead and I'm new to node.js. Thanks for the suggests too, I'm using underscore already, but I just couldn't apply it to this challenge cus I was still unclear about the problem I needed to solve. – Chibuzo Feb 06 '16 at 23:15
  • The object obj itself was an array so I had to use obj[0].data I've inserted into the database. Thank you very much! – Chibuzo Feb 07 '16 at 11:44