I have array of objects, in which the keys matches,
then return the array of object using javascript
for example,
Passing param zapp is found in arr, return those objects in array using
javascript
var arr =[
{id:1, keys: "zapp"},
{id:2, keys: "zapp, zoin"},
{id:3, keys: "dell"}
];
tried
function checkKeys (arr, keytocheck) {
let result=[]
if(arr.length > 0){
for(let elem of arr) {
const splitkeys = elem.keys(',');
if(elem.keys.indexOf(',') > -1) {
// comma have
if(splitKeys.includes(keytocheck)){
result.push(elem);
}
}
else {
// no comma
if(elem.keys === keytocheck) {
result.push(elem);
}
}
}
}
return result
};
console.log(checkKeys(arr, "zapp"));
Expected Output
[
{id:1, keys: "zapp"},
{id:2, keys: "zapp, zoin"}
]