3

I get issues when I want to loop through a JSON array of objects.

Issues such as:

  • It only counts two (I assume because of they Object.keys) and I have two keys.
  • Loops with only one value

My code:

var codes = require('./nl.json');
for (var i = 0, l = Object.keys(codes).length; i <= l; i++) {
    console.log(l) ;
    var areaCodeTest = codes.netherlands[i].areaCode;
    var areaNameTest = codes.netherlands[i].areaName;

    it("Search for postal code ", function(){
        var postCode = element(by.id("imysearchstring"));
        postCode.click(); 
        browser.sleep(1000); 
        console.log(areaCodeTest);
        postCode.clear().sendKeys(areaCodeTest);
        browser.sleep(1000);
        console.log("Typed " + areaCodeTest);
    });
}

My Json (Short example):

{
"netherlands": [
  {
    "areaCode": 9401,
    "areaName": "Assen"
  },
  {
    "areaCode": 9402,
    "areaName": "Assen"
  },
  {
    "areaCode": 9403,
    "areaName": "Assen"
  }
 ]
}

I have looked at answers such as :

Size of Object and Length of Json

I have tried:

(var i = 0, l = Object.keys(codes).length; i <= l; i++)

(var i = 0, l = Object.keys(codes.netherlands[0]).length; i <= l; i++)

for (var i = 0, l = codes.netherlands.length; i <= l; i++) // uses last areaCode in json file and only loop with that number. It does not start from top.

Image: some of my outputs

Expected: What I want is to count amount of ofjects in JSON (Not the key/values)

Loop through all data and assign them to var areaCodeTest = codes.netherlands[i].areaCode; and var areaNameTest = codes.netherlands[i].areaName;

3
  • 3
    Do you just want the length of the array? if so, it should be codes.netherlands.length in your case Commented Aug 10, 2019 at 12:09
  • @ShakedDahan it then loops only using one areaCode (8245) - Last record in JSON file. I want it to start from the top and go through each object and use areaName and areaCode Commented Aug 10, 2019 at 12:15
  • why don&#39;t you use a more comfy loop? Object.keys(codes).forEach(function (item) { console.log(item); // key console.log(codes[areaCode]; // value }); Commented Aug 10, 2019 at 12:16

3 Answers 3

1

I got it to work by using the following:

var codes = require('./nl.json');

codes.forEach((item) => {
    var areaCodeTest = item.areaCode;
    var areaNameTest = item.areaName;

    it("and search for postal code ", function(){
        var postCode = element(by.id("imysearchstring"));
        postCode.click(); 
        console.log(areaCodeTest);
        postCode.clear().sendKeys(areaCodeTest);
        browser.sleep(1000);
        console.log("Typed " + areaCodeTest);
    });
}

I am not a 100% what the => means near the foreach but I am currently researching why my code works. If you know please post a comment so that other developers also learn.

This let me think of the meme "not sure why code does not work / Not sure why code works"

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

1 Comment

0

You need to access the actual key in your loop in order to access codes[key]

Simplified version of your for() loop with stored variable for the object keys or using for in loop

const keys = Object.keys(codes)

for (let i = 0; i < keys.length; i++) {
  // current object key and value of that property in main object
  const key = keys[i], arr = codes[key];
  console.log(`key = ${key}, length= ${arr.length}`)

  // do another loop here for `arr` if needed
}

// OR using `for in`
for (let key in codes) {
  console.log(`key = ${key}, length= ${codes[key].length}`)
}
<script>
  const codes = {
    "netherlands": [{
        "areaCode": 9401,
        "areaName": "Assen"
      },
      {
        "areaCode": 9402,
        "areaName": "Assen"
      },
      {
        "areaCode": 9403,
        "areaName": "Assen"
      }
    ]
  }
</script>

Comments

-1

Try this I give you a sample

const object1 = {
  a: 'somestring',
  b: 42,
  c: false
};

var length = (Object.keys(object1).length);

Please Refer this Link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

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.