-1

I'm trying to use two arrays to populate a couple of each statements instead of writing it out 20 times for every attribute.

Here's what I was thinking. The idea here is to get the data (stored in mainData). I need a number of objects from this data, so I put those in an array ($dataPoints). I'm taking all of this data an appending it to a table. I'm adding the data to specific rows with specific ids (stored in $tableIds).

So, the basic idea...for every item in $dataPoints, look up that part of the data and append it to the table in the place indicated by the ids in the same position in the array. Like so...

var $dataPoints = ['name','description','subtitle'];
var $tableIds = ['mNames','mDescription','mSubtitle'];
$.each($dataPoints, function(index, val) {
    $.each(mainData.val[index].source_values, function(index, val) {
        $("#matrix_datatable").find('#'+$tableIds[index])
          .append($('<td class="tableCells">')
          .append(val.value + '</td>')
        )
    });
});

However, this line:

$.each(mainData.val[index].source_values, function(index, val) {

is throwing an error:

Uncaught TypeError: Cannot read property '0' of undefined

What am I missing here? Is there a simpler way to do this?

EDIT: Note that this works perfectly.

$.each(mainData.subtitle.source_values, function(index, val) {
    $("#matrix_datatable").find('#mSubtitle')
      .append($('<td class="tableCells">')
      .append(val.value + '</td>')
    )
});

However, I don't want to write this out for the 20 different objects within mainData.

juanferrer
  • 1,192
  • 1
  • 16
  • 29
jonmrich
  • 4,233
  • 5
  • 42
  • 94
  • 1
    Please post sample data – baao Aug 03 '17 at 14:22
  • Is `mainData.val` an array of length > 1? – Brett DeWoody Aug 03 '17 at 14:23
  • We cannot help you with code we cannot see. Clearly, if it's blowing up on that line, `mainData.val` has no `"0"` property. – T.J. Crowder Aug 03 '17 at 14:24
  • 3
    This is a duplicate of http://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable. You want `mainData[val]`, not `mainData.val`. – T.J. Crowder Aug 03 '17 at 14:25
  • Possible duplicate of [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – Louys Patrice Bessette Aug 03 '17 at 14:27
  • @T.J.Crowder Thanks. This got me past the errors, but the data populates the table in the wrong positions. Instead of going across rows for the same attribute, it's just going down columns and starting at the top of the next column once the rows are all filled. – jonmrich Aug 03 '17 at 14:30
  • 1
    @jonmrich: I suggest deleting this question, doing a bit more debugging (stepping through the code with your debugger, etc.), and if you still can't work it out, posting a new question with a **runnable** [mcve] using Stack Snippets (the `[<>]` toolbar button). – T.J. Crowder Aug 03 '17 at 14:31
  • @jonmrich: You not using the right `index` in the second each. That is the index of the first `each()` loop... Not the index of `mainData`. I would try `$.each(mainData[val].source_values` at first... Maybe you don't even need to provide an `index`... Can't be sure without seeing the json structure (Which you should post a sample). – Louys Patrice Bessette Aug 03 '17 at 14:37

1 Answers1

0

It looks like you probably meant:

mainData[val][index].source_values

Writing mainData.val literally looks for a key val in mainData.

VoteyDisciple
  • 37,319
  • 5
  • 97
  • 97