0
        component.set("v.filePushJobIds", actionResult.getReturnValue());
        var jobsIds = component.get("v.filePushJobIds");
        console.log(jobsIds);
        console.log(jobsIds.size);

I see that my map prints out fine on its own ie.

{707g0000028jBClAAM: "069g0000001QJk9AAG", 707g0000028jBCmAAM: "069g0000001QJjcAAG", 707g0000028jBCnAAM: "069g0000001QJofAAG", 707g0000028jBCoAAM: "069g0000001QJjbAAG", 707g0000028jBGcAAM: "069g0000001QJlXAAW", …}

but for some reason i cannot get the size of this map. I've looked up how to do this in JS and SF and have tried size, length, size(), i've tried iterating through the map and no luck. Either returns undefined or errors out. Any ideas?

0

2 Answers 2

4

Based on what you pasted your map is represented as a single object with named attributes.

This object in JS has no implementation of .size so it wont show you the size.

You could try something like:

console.log(Reflect.ownKeys(jobsIds).length);

Reflect.ownKeys(object) should convert your object into an array allowing length and other array functions such as forEach to work. Then you would do something like:

let keys = Reflect.ownKeys(jobIds);

keys.forEach(function(key) {
    console.log(key);
    console.log(jobIds[key]);
    // ... do more stuff
}, this);

Keep in mind that keys only contains the attribute names, you will have to do object[keys[0]] or something like above to be able to get to the values.

1
4

You can check the length of the keys the object owns:

console.log(Object.keys(jobIds).length);
3
  • Hi @sfdcfox , How to check whether object has a key or not ? Commented Feb 18, 2020 at 18:40
  • @amidstCloud if(key in varName) gives you true if it has a given property anywhere in its prototype chain, while if(varName.hasOwnProperty(key)) will tell you if that property directly exists on an object without checking the prototype chain.
    – sfdcfox
    Commented Feb 18, 2020 at 19:13
  • thanks a lot.. I am returning the a map<Id,Integer> from Apex class and storing it in a var in the component controller. I was looking for a way to check size/length of it , but both are coming as undefined Commented Feb 18, 2020 at 19:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .