Page 1 of 1
count arrays in js
Posted: 31 July 2020, 10:40
by Ginso
hello,
when i send arrays to my js file like in the getAllDatas or with notifyAllPlayers, the objects that js gets are not really arrays.
How can i get the number of entries there?
Re: count arrays in js
Posted: 31 July 2020, 11:22
by Tisaac
Just use array_values in your php code when sending arrays so that js get them as array and not object.
Re: count arrays in js
Posted: 31 July 2020, 11:27
by Lunalol
Or use Object.keys(yourCollection).length
Re: count arrays in js
Posted: 31 July 2020, 11:36
by joezg
If you send php associative array, you get an object in js.
You can get values of object properties as array with
Code: Select all
Object.values(gamedatas.something)
Now you have array and you can do with him anything you will do with arrays:
Code: Select all
let something = Object.values(gamedatas.something);
console.length(something.length);
something.forEach(item => console.log(item));
More info about Object.values is here:
https://developer.mozilla.org/en/docs/W ... ect/values
Re: count arrays in js
Posted: 31 July 2020, 13:46
by Tisaac
Lunalol wrote: ↑31 July 2020, 11:27
Or use Object.keys(yourCollection).length
Sure but then what the point of sending associative array if you just use the values ?