I am writing a function that will iterate through an of objects to find if key:value pair exists and if so, it adds into another array (in case there are multiple matches)
The array contains objects. The function takes as argument what key and value you're looking for and then iterates through array till it finds it. As you can see, key is a variable then and my problem is I am not sure how to access object property when referring to as a variable value.
for (let i = 0; i < arr.length; i++) {
if (arr[i].[key] == value) {
result.push(arr[i]);
}
}
I thought maybe putting it in [key] would help, but I was obviously wrong. I tried googling how to do it, but I guess i can't put it in words right .