1

I have an array of objects with below value

var arr = 
[{"Id":"0QL8W000006RHl1WAG","Sortorder":"1"}, 
          {"Id":"0QL8W000006RHl0WAG","Sortorder":"2"}, 
           {"Id":"0QL8W000006RHl2WAG","Sortorder":"3"}
          ]

Now I want to convert it to an object with below format.

var arr = {0QL8W000006RHl1WAG=1,0QL8W000006RHl0WAG=2,0QL8W000006RHl2WAG=3}

I have really tried most of the javascript methods for this with no success.

Please help on this. It's really very very urgent.

2 Answers 2

0

For the desired output , you essentially need a JS Map data structure.

Why are we using a Map over a JS object : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#objects_vs._maps

Step 1 : Convert each object in the array to an array itself using the map method :

const arrayObj = [{"Id":"0QL8W000006RHl1WAG","Sortorder":"1"}, {"Id":"0QL8W000006RHl0WAG","Sortorder":"2"}, {"Id":"0QL8W000006RHl2WAG","Sortorder":"3"} ];

const mappedArrayObj = arrayObj.map(obj => [obj.Id, obj.Sortorder]);

Our 1st step produced an array of array(mappedArrayObj) as below:

[
  [ '0QL8W000006RHl1WAG', '1' ],
  [ '0QL8W000006RHl0WAG', '2' ],
  [ '0QL8W000006RHl2WAG', '3' ]
]

Step 2 : Convert the mappedArrayObj to a JS map using Map constructor as below :

const mapOfObjects = new Map(mappedArrayObj); 

This will produce output (console.log(mapOfObjects);) as below :

{
  '0QL8W000006RHl1WAG' => '1',
  '0QL8W000006RHl0WAG' => '2',
  '0QL8W000006RHl2WAG' => '3'
}
9
  • Thank you for the quick response, but it's not working fine. I've tried the above code but in console it;s showing [object Map] I've tried it like this const map = new Map(component.get("v.StoreSortOrder").map((obj) => [obj.Id, obj.Sortorder])); alert('map11----'+map); Output - map11---- [object Map] Please help Mar 12, 2023 at 11:00
  • alert & console function will stringify the object and hence , you are observing [object Map] try with : console.log("output ", map) -> Notice the comma in the function.
    – Rohit
    Mar 12, 2023 at 11:06
  • You can check this link : onecompiler.com/javascript/3z2e8w2ct
    – Rohit
    Mar 12, 2023 at 11:11
  • Thanks a ton.. It's working perfect. Getting below output in the console {'0QL8W000006RHl1WAG' => '1', '0QL8W000006RHl0WAG' => '4', '0QL8W000006RHl2WAG' => '5'} I need to do one more thing now. Need to remove the quotes mark around the key/value pair. Hence the final output should be {0QL8W000006RHl1WAG=>1, 0QL8W000006RHl0WAG=>4,0QL8W000006RHl2WAG=> 5} Please guide on this as well. Mar 12, 2023 at 11:17
  • 1
    I got this. In Salesforce, I can use Id.valueOf(String) to convert this into Id. Thanks a ton Rohit for your help :) Mar 12, 2023 at 11:32
2

An alternative to @Rohit's approach is to use Array.reduce functionality:

const arrayObj = [{"Id":"0QL8W000006RHl1WAG","Sortorder":"1"}, {"Id":"0QL8W000006RHl0WAG","Sortorder":"2"}, {"Id":"0QL8W000006RHl2WAG","Sortorder":"3"} ];

const result = arrayObj.reduce(
    (target, item) => (target[item.Id] = item.Sortorder, target),
    {});

The {} is the initial empty object to return (what becomes the target in the reducer), while the "reducer function" simply:

  1. Sets the property on that object with the name taken from the item's Id and sets it to the item's Sortorder (target[item.Id] = item.Sortorder) then
  2. Uses the comma operator to ensure that the function returns the target object for the reducer chaining (, target).

By "chaining" the target object in this way, this avoids creating garbage that must be collected - we simply add properties to that one JavaScript object.

1
  • Nice approach @phil 👍
    – Rohit
    Mar 12, 2023 at 12:01

You must log in to answer this question.

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