count arrays in js

Game development with Board Game Arena Studio
Post Reply
User avatar
Ginso
Posts: 26
Joined: 16 July 2020, 19:45

count arrays in js

Post 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?
User avatar
Tisaac
Posts: 2743
Joined: 26 August 2014, 21:28

Re: count arrays in js

Post by Tisaac »

Just use array_values in your php code when sending arrays so that js get them as array and not object.
User avatar
Lunalol
Posts: 584
Joined: 09 October 2016, 23:21

Re: count arrays in js

Post by Lunalol »

Or use Object.keys(yourCollection).length
User avatar
joezg
Posts: 70
Joined: 16 June 2011, 17:17

Re: count arrays in js

Post 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
User avatar
Tisaac
Posts: 2743
Joined: 26 August 2014, 21:28

Re: count arrays in js

Post 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 ?
Post Reply

Return to “Developers”