FCF 2.0 development in progress...
> > > >
[News] [Packages API] [Downloads] [Donate to the project] [Contacts]

fcf.count() function

integer fcf.count(string|{}|[]|Map|Set a_object)
integer fcf.count(string|{}|[]|Map|Set a_object, function a_cb)

Package: fcf-framework-core

File: fcf-framework-core:fcf.js

Available from version: 2.0.2

Determines the number of elements in an object or a string. The function works both with simple objects and with arrays, Map and Set

Arguments

string|{}|[]|Map|Set a_object
- Source object

function a_cb
- If a function is given, then a_cb is called sequentially for each element of the array to determine the number of elements, and if the function returns true, then the element is counted in the total subset.

Function signature: boolean a_cb(mixed a_key, mixed a_value)

Result
integer
- Number of counted elements in an object or a string

If the passed argument is not an object or a string, returns 0

Example: Function application

console.log(`[1,2,3] :`, fcf.count([1,2,3])); console.log(`{"k1": "v1"} :`, fcf.count({"k1": "v1"})); { let map = new Map(); map.set("k1", "v1"); console.log(`Map({"k1": "v1"}) :`, fcf.count(map)); } { let result = fcf.count([1,2,3,4], (a_key,a_value) => { return a_value % 2 == 0; }); console.log(`[1,2,3,4] if value%2 == 0 :`, result); }

Output:

[1,2,3] : 3 {"k1": "v1"} : 1 Map({"k1": "v1"}) : 1 [1,2,3,4] if value%2 == 0 : 2