//https://www.tutorialspoint.com/filter-array-based-on-another-array-in-javascript const jsonResponse=[{ "id": 1, "code": "Target0", "cost": 24 }, { "id": 2, "code": "Target1", "cost": 23 }, { "id": 3,"code": "test", "cost": 23 }, { "id": 4, "code": "Target2", "cost": 23 }, , { "id": 5, "code": "Maniac", "cost": 9 } ] const filterArray =['Target0','Target1','Target2'] const getObjectsFromjsonResponseBasedOnfilterArray =(arr1,arr2)=>{ const newArray=filterArray.filter(x=>{ let indexPos=x.indexOf(jsonResponse.code); return indexPos>-1?jsonResponse[indexPos]:[] }) console.log(newArray); }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter