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;

ruan
  • 65
  • 1
  • 5
  • 3
    Do you just want the length of the array? if so, it should be `codes.netherlands.length` in your case – Shaked Dahan Aug 10 '19 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 – ruan Aug 10 '19 at 12:15
  • why don't you use a more comfy loop? Object.keys(codes).forEach(function (item) { console.log(item); // key console.log(codes[areaCode]; // value }); – Raphael Aug 10 '19 at 12:16

3 Answers3

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"

ruan
  • 65
  • 1
  • 5
  • 1
    https://stackoverflow.com/questions/24900875/whats-the-meaning-of-an-arrow-formed-from-equals-greater-than-in-javas – AuxTaco Aug 11 '19 at 09:22
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>
charlietfl
  • 170,828
  • 13
  • 121
  • 150
-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

heyharshil
  • 177
  • 8