0

Try to get length of array inside an object

enter image description here

image above is return of Res

I want to check if every tenor length is equal or more than zero.

But, I stuck in how to call itemCounter"i" when loop it

var tenorCount = Object.keys(res).length;
var flagTenor = false;
try{
    
    for(var i=1;i<tenorCount;i++){ 
        
            if (res.itemCounter_+i.tenor.length < 1){ //Stuck in here
                flagTenor = true;
            }
        
    }
}catch(e){
    console.log(e);
}

How to call itemCounter"i" correctly?

lauwis
  • 323
  • 1
  • 4
  • 15

1 Answers1

0

Try like this using template literals and computed property names

if (res[`itemCounter_${i+1}`].tenor.length < 1)

Example

const res = {
  itemCounter_1: {
    tenor: ["one"]
  },
  itemCounter_2: {
    tenor: []
  },
}
var tenorCount = Object.keys(res).length;
var flagTenor = false;
try {

  for (var i = 0; i < tenorCount; i++) {

    if (res[`itemCounter_${i+1}`].tenor.length < 1) {
      flagTenor = true;
    }

  }
} catch (e) {
  console.log(e);
}

console.log(flagTenor);
kiranvj
  • 32,342
  • 7
  • 71
  • 76