Answered by:
Using a High order Function to return Array of Key:Value pairs

Question
-
User438705957 posted
I have incoming JSON which is an array of objects.
Each object has these properties.
{ keyname : "@keyName",
@keyName : value,
fullName : value}
A typical example of one of the array elements is:
{ keyname: "Employee_Id",
Employee_Id : 50013135,
fullName: "Wilfred Dare"}
So the object contains the name of the key ("Employee_Id") in one of the properties, along with the value of the key ("50013135") in another property, as well as the fullName associated with the key value("Wilfred Dare").
I wish to turn the array of objects into an array of key:value pairs derived from each element in the array.
So in the above example, the corresponding element in the array would be [ 50013135 ] : "Wilfred Dare"
The value of the key is the index of the array.The resulting array would look like:
[50013135]:"Wilfred Dare"
[66699999]:"The Joker"
[36374890]:"Cat Woman"I have achieved this using a clunky for loop, but wish to do it using .map or some other high order function.
I have not been able to achieve this so far.
Thanks for considering
Tuesday, April 9, 2019 11:33 PM
Answers
-
User839733648 posted
Hi Madog,
According to your description, I suggest that you could use Object.keys() and .map() to achieve your requirement.
Here is my testing code.
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script> $(function () { var obj = [{ keyname: "Employee_Id", Employee_Id: 50013135, fullName: "Wilfred Dare" }, { keyname: "Employee_Id", Employee_Id: 66699999, fullName: "The Joker" }, { keyname: "Employee_Id", Employee_Id: 36374890, fullName: "Cat Woman" }]; var result = Object.keys(obj).map(function (key) { return '['+obj[key].Employee_Id+']' + ':"' + obj[key].fullName+'"'; }); console.log(result); }) </script> </head> <body> </body> </html>
result:
Best Regards,
Jenifer
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, April 10, 2019 6:53 AM
All replies
-
User839733648 posted
Hi Madog,
According to your description, I suggest that you could use Object.keys() and .map() to achieve your requirement.
Here is my testing code.
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script> $(function () { var obj = [{ keyname: "Employee_Id", Employee_Id: 50013135, fullName: "Wilfred Dare" }, { keyname: "Employee_Id", Employee_Id: 66699999, fullName: "The Joker" }, { keyname: "Employee_Id", Employee_Id: 36374890, fullName: "Cat Woman" }]; var result = Object.keys(obj).map(function (key) { return '['+obj[key].Employee_Id+']' + ':"' + obj[key].fullName+'"'; }); console.log(result); }) </script> </head> <body> </body> </html>
result:
Best Regards,
Jenifer
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, April 10, 2019 6:53 AM -
User438705957 posted
Thanks so much Jenifer,<br>
That’s an amazing amount of effort just to answer a complete strangers question.Wednesday, April 10, 2019 8:36 AM -
User438705957 posted
It's amazing how the extensive research I do before posting a question, is nowhere near as effective as the scant research I do after posting a question.
I have come up with a slightly different way which creates a Map I later iterate thru to create a drop-down of options, which are in effect the key:value pair.
This covers the generic case where the incoming data can be from any EF POCO that has a foreign table collection.
var optionsMap = new Map(); Object.keys(data).forEach(function (key) { keyVal = data[key][data[key].keyName]; // resolves to data[key]['@TheNameOfTheKey'] e.g. data[0]["Location_Id"] val = data[key].FullName; optionsMap.set(keyVal, val); });
optionsMap.forEach(function (value, key) { $selectOption = $('<option/>').attr({ 'value': key }).text(value); $inputFor.append($selectOption); });
Cheers
MadogWednesday, April 10, 2019 11:07 PM