0

How do I pick the first value of rowVal of the first object (I want to print 42.00).

dataTest.rows = [{
  "rowHeader": "",
  "rowDesc": ["Gene Name"],
  "rowVal": [
    ["42.00", "57.00", "45.00", "48.00", "52.00", "47.00", "39.00", "38.00", "35.00"]
  ]
}, {
  "rowHeader": "",
  "rowDesc": ["Gene Source"],
  "rowVal": [
    ["38.00", "50.00", "39.00", "41.00", "45.00", "40.00", "34.00", "33.00", "29.00"]

  ]
}];

I tried:

console.log(dataTest.rows[0].rowVal[0]);

but it returns all the numbers in the array of rowVal of the first object?

5
  • 1
    console.log(dataTest.rows[0].rowVal[0][0]); Commented Apr 19, 2018 at 3:37
  • rowVal is an array of arrays, not an array of primitives: you're selecting a sub-array, not a string inside the sub-array. Go down one more level to access the first element: console.log(dataTest.rows[0].rowVal[0][0]); Commented Apr 19, 2018 at 3:37
  • console.log(dataTest.rows[0].rowVal[0][0]); You were so close. Note the extra [0] Commented Apr 19, 2018 at 3:37
  • Your rowVal is an array with an array as the first value. Note the [[ Commented Apr 19, 2018 at 3:37
  • thank you.. I see.. i am building the code slowly to be able to load a json file into a table.. i still have some work to do before i get there Commented Apr 19, 2018 at 3:42

1 Answer 1

1

rowVal is an Array of Arrays. That's why you get that result.

    "rowVal": [
        ["38.00", "50.00", "39.00", "41.00", "45.00", "40.00", "34.00", "33.00", "29.00"]

    ]

See, when you say rowVal[0], it refers to an array. So, ideally, it should be something like

    "rowVal": ["38.00", "50.00", "39.00", "41.00", "45.00", "40.00", "34.00", "33.00", "29.00"]

if you want to access the elements as rowVal[0]. Or else, you have to change your reference to rowVal[0][0].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.