1

I'm trying to make a variable using data from a JSON file, though it keeps showing as undefined. inspection shows the options object with the following error:

The goal from this code is to configure a product using the first JSON object and then pull in extra information about the product options using the second json object.

The code should output the title of

"cover-silk": ["150gsm-silk-cover"] 

which is stored in the second JSON object

The variable childTitle should = "150gsm"

productData = {
  "product": [{
    "name": "booklet",
    "section": [{
      "coverStock": {
        "cover-silk": ["150gsm-silk-cover"]
      }
    }]
  }]
}, {
  "section2": {
    "coverStock": [{
      "title": "Cover Stock",
      "options": [{
        "cover-silk": {
          "title": "Silk",
          "childOptions": {
            "150gsm-silk-cover": {
              "title": "150gsm"
            }
          }
        }
      }]
    }]
  }
};

var key = productData.product[0].section[0];
var keys = [];
for (var k in key) keys.push(k);

var len = keys.length;
for (var i = 0; i < len; i++) {

var option = productData.product[0].section[0][keys[i]];

  var secObj = (option);
  var secObjArray = [];
  for (var k2 in secObj) secObjArray.push(k2);

  var len2 = secObjArray.length;

  for (var j = 0; j < len2; j++) {

    var childTitle = productData.section2[keys[i]][0].options[0][secObjArray][j].title;
    console.log(childTitle);

  }
};

3
  • title is a property of cover-silk: productData.section2.coverStock[0].options[0]['cover-silk'].title Commented Apr 7, 2018 at 23:34
  • Where's the actual code that throws the error? Take a few minutes to read minimal reproducible example Commented Apr 7, 2018 at 23:35
  • Thanks for the advice, i've updated the code Commented Apr 8, 2018 at 0:30

1 Answer 1

3

I have made a lot of changes. The whole structure wasn't right. Check out the comments below and working snippet too:

  1. The indices are all wrong.
  2. The original JSON was wrong.
  3. Changed a lot. Better to use Diff.

Snippet

var productData = {
  "product": [{
    "name": "booklet",
    "section": [{
      "coverStock": {
        "cover-silk": ["200gsm-silk-cover", "250gsm-silk-cover"],
        "cover-gloss": ["300gsm-gloss-cover", "250gsm-gloss-cover"]
      }
    }],
    "section2": {
      "coverStock": [{
        "title": "Cover Stock",
        "options": [{
          "cover-silk": {
            "title": "Silk",
            "childOptions": {
              "150gsm-silk-cover": {
                "title": "150gsm"
              }
            }
          }
        }]
      }]
    }
  }]
};

var key = productData.product[0].section[0];
var keys = [];
for (var k in key)
  keys.push(k);
var len = keys.length;
for (var i = 0; i < len; i++) {
  var option = productData.product[0].section[0][keys[i]];
  var secObj = option;
  var secObjArray = [];
  for (var k2 in secObj)
    secObjArray.push(k2);
  var len2 = secObjArray.length;
  for (j = 0; j < len2; j++) {
    var childTitle = productData.product[0].section[0][keys[i]][secObjArray[j]][0];
    console.log(childTitle);
  }
}

Diff:

If you are interested, this is the Diff:

Diff

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

14 Comments

Apologies, I missed that bit whilst trying to simplify the code .cover-silk is part of the reference [edited question]
@Nate My answer stays the same. When you want to use a property when it has - or some special characters, use the array notation, and not the dot notation. Check my answer.
My actual code uses array notation: childTitle = productData.section2[keys[i]][0].options[0][secObjArray][j]; It seems to be the .options[0] causing the error
@Nate Would you like to show us the full code and replicate the error please?
@Nate You can edit your own question to add the full code. Please do it. :) ps: I am also from UK. :)
|

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.