0

I have a searchContact method in the helper. I get from the component a list of contacts of type object and try to convert it to an array. I want to be able, after conversion, to refer to each element of the array by index and get the value of the "Name" field. But I don't understand why the conversion function .fromEntries () is not working? And I am getting the value of the contacts variable as an [object Object].

searchContact : function(component, searchKeyword, helper){
    let contacts = Object.fromEntries(component.get('v.contacts'));
    ...
},

1 Answer 1

1

Object.fromEntries converts a key-value pair list (e.g. a Map) to an Object. It appears that you want the opposite; to create an array from an Object, you want Object.entries, as in:

let contacts = Object.entries(component.get("v.contacts"));

Just knowing you have an Object, though, it's hard to tell what you're trying to do. Check console.log(JSON.stringify(component.get("v.contacts"))); to see what your data looks like.

8
  • My data looks like this: [ {"Name":"Jack Johns","Email":"[email protected]","Contact_Level__c":"Primary","AccountId":"sdfsd54654sdf899465d4f", "OwnerId":"kjhk565465sd6f45sd65f4SAD","CreatedById":"kjhk565465sd6f45sd65f4SAD","CreatedDate":"2021-02-07T20:49:46.000Z", "Id":"sdfsdseLKFekefffEJK","Account":{"Name":"Oil & Gas","Id":"eKJNKJenkjfnKJEF5e4fee"}, "Owner":{"Name":"Jack Smile","Id":"8798dfsdf798798FDS"},"CreatedBy":{"Name":"Jack Smile", "Id":"8798dfsdf798798FDS"},"AccountName":"Oil & Gas","OwnerName":"Jack Smile", "CreatedByName":"Jack Smile"}, {"Name":...} ] Commented Mar 16, 2021 at 17:31
  • Converting them through Object.entries(component.get("v.contacts")), i get this (console.log(contacts)): 0,[object Object],1,[object Object],2,[object Object],3,[object Object],4,[object Object],5,[object Object], ... Commented Mar 16, 2021 at 17:37
  • @uniqueuser So it is an array. But what exactly are you trying to do? It looks like the data is already in the format you desire. This may be an X-Y Problem.
    – sfdcfox
    Commented Mar 16, 2021 at 18:23
  • If I convert an object to an array using the method you suggested, then the array will consist of objects of type object, and I still won't get the value of the "Name" field of each element. And if we transform, for example, in this way each object in the loop: Object.entries (component.get ('v.contacts') [0]), then I get `Name,Jack Johns,Email,[email protected],Contact_Level__c,Primary,AccountId,sdfsd54654sdf899465d4f, ... this will already be a string with the field name and its value in the string. Again, it is not possible to get the value of the "Name" field. Commented Mar 17, 2021 at 11:12
  • @uniqueuser Yes, but what exactly are you trying to do?
    – sfdcfox
    Commented Mar 17, 2021 at 11:18

You must log in to answer this question.

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