0

I noticed something unusual today and wondered of anyone had any ideas of explanantion. I am testing the length of an array in NodeJS which is populated within a for loop. Something like the code snippet below.

// Set up an array to hold the IDS
var ids = []
// Iterate through each of the devices 
for (let i = 0; i < devices.length; i++) {
  let id = devices[i].deviceManufacturerCode
  if (id == qubino_id){
    ids[devices[i].label] = id
   }
}
console.log(ids,ids.length)

The output from the console.log after the for loop is iterated through is:

[ 'Qubino Energy Monitor': '0159-0007-0052' ] 0

The elements in the array are what I expected, a key value pair of the device.label and the id of the device. The length of 0 is not expected though as there is an entry in the array.

I changed the loop to append values instead:

// Set up an array to hold the IDS
var ids = []
// Iterate through each of the devices 
for (let i = 0; i < devices.length; i++) {
  let id = devices[i].deviceManufacturerCode
  if (id == qubino_id){
    ids.push(id) // CHANGE HERE 
   }
}
console.log(ids,ids.length)

The output from the console.log after the for loop is iterated through is now:

[ '0159-0007-0052' ] 1

The output of length has now increased to 1 which is expected and I have dropped the key from the array entry so everything is correct.

If I want to get the key, value object in the array and the length it increase I have to create the object first then push it to the array.

// Set up an array to hold the IDS
var ids = []
// Iterate through each of the devices 
for (let i = 0; i < devices.length; i++) {
  let id = devices[i].deviceManufacturerCode
  if (id == qubino_id){
    let name = devices[i].label
    let obj = {}
    obj[name] =  id  
    ids.push(obj)
   }
}
console.log(ids,ids.length)

The console.log now returns:

[ { 'Qubino Energy Monitor': '0159-0007-0052' } ] 1

As expected an object at index 0, making the array length of 1.

Why in the first case am I getting a length of 0 for the array?

WK123
  • 620
  • 7
  • 18

3 Answers3

2

Is label prop contains numbers? An array supposed to work with numbers internally converted to strings (just like keys in ordinary JS-object). Compare:

let array = []
arr[3] = 1 // arr.length = 4
let array = []
arr['abc'] = 2 // arr.length = 0
Anatoly
  • 20,799
  • 3
  • 28
  • 42
  • I think this is the problem, trying to index array with a string instead of indexing with a number. Just to me I would of expected your second snippet to have a length of 1 because there is something in it. – WK123 Nov 12 '20 at 16:11
  • In the second example you still can access `arr['abc']. If you need to store by a key try to use a usual object for that. – Anatoly Nov 12 '20 at 16:15
  • Yeah I could still get at it but I wanted to check if the array was empty and this is how I came across this problem when testing if length of array was 0 when using second case, I've just converted it to pure array with numerical index. – WK123 Nov 12 '20 at 16:17
1

You are treating an array like a dictionary when you

ids[devices[i].label] = id

Check out the simplified snippet:

const devices = [{deviceManufacturerCode:'100-01', label: 'foo'}]

// Set up an array to hold the IDS
var ids = []
// Iterate through each of the devices 
for (let i = 0; i < devices.length; i++) {
  let id = devices[i].deviceManufacturerCode
  ids[devices[i].label] = id
}
console.log(ids,ids.length)

That console.log statement seems wrong, I don't see how you got that.

[ 'Qubino Energy Monitor': '0159-0007-0052' ] 
Robert Moskal
  • 21,737
  • 8
  • 62
  • 86
  • Yeah I think I have just mixed up numerical indexing and key/value. I do get that output though I checked my code again and its the same, I'm running node v14.15.0 – WK123 Nov 12 '20 at 16:14
0

That is a perfectly usual behaviour. Because in the first example you are not adding items to an array but adding a property to the "ids" object. If you would for example do something like this you would correctly index the array:

var arr = []
arr[1] = "foo"
console.log(arr.length)
// output: 2

Also see: Why does a string index in an array not increase the 'length'?

MatthiasV
  • 71
  • 2
  • 4