Just starting out with typescript in angular 2 and encountering a bit of a challenge. Here's the situation:
needle json
[{"empId":100,"orgId":500}
{"empId":201,"orgId":566}]
The above json is structured in a specific order, and we need to preserve that order when searching for matching values in another json array (Haystack).
Haystack json array
[
{"empCode":21,"fname":"Ashish","Lname":"Shukla"},
{"empCode":22,"fname":"John","Lname":"Mark"},
{"empCode":21,"fname":"Vigil","Lname":"Rocker"},
{"empCode":201,"fname":"Rick","Lname":"Mandez"},
{"empCode":21,"fname":"Erik","Lname":"Francis"},
{"empCode":100,"fname":"Alex","Lname":"Mishra"},
{"empCode":21,"fname":"Feeder","Lname":"Kapoor"},
{"empCode":21,"fname":"Dan","Lname":"Rox"},
{"empCode":21,"fname":"Herb","Lname":"Deen"},
{"empCode":21,"fname":"Nate","Lname":"Diaz"},
{"empCode":21,"fname":"Nick","Lname":"Diaz"},
{"empCode":21,"fname":"Conor","Lname":"Pussy"}
]
My goal is to extract the values from the haystack array that match the ids in the needle array, while preserving the order specified in the needle.
{"empCode":100,"fname":"Alex","Lname":"Mishra"},
{"empCode":201,"fname":"Rick","Lname":"Mandez"}
I have a solution in place, but I suspect it may not be the most efficient as it involves multiple loops. Any suggestions for a more optimal approach?
NOTE: It's crucial to maintain the order of employee IDs in the result json as per the needle json.
Thank you very much :)