I have two arrays:
a = [{"sourceId": "1", "targetId": "2", "name": "heats air"} ,
{"sourceId": "3", "targetId": "4", "name": "power"}]
b = [{"name": "Hair Dryer", "id": "1"},
{"name": "Heating System", "id": "2"},
{"name": "Mains", "id": "3"},
{"name": "Blower", "id": "4"}]
How do I get the output like this:
[{"sourceId": "1", "targetId": "2", "name": "heats air", "from": "Hair Dryer", "to": "Heating System"},
{"sourceId": "3", "targetId": "4", "name": "power","from": "Mains", "to": "Blower"]
I want to merge them based on the property values: the keys "sourceId" and "targetId" of array a should correspond to the key "id" of array b. If a sourceId is matched with an id, add the value of the name with key "from" to the object in array a; If a targetId is matched with an id, add the value of the name with key "to" to the item in array a. Also,I am wondering whether I can do this without using lodash. (using ES6)
fromandtoshould be set to?fromandtoare supposed to be set. Comments should not be used for required information - that information should be added to the question itself.