I'm writing a code that has a requirement of filtering an array of objects with a string and creating a new array based on the filtered value.
Here is my code.
var a = [{
"label": "June - 2021",
"value": "June"
}, {
"label": "May - 2021",
"value": "May"
}, {
"label": "April - 2021",
"value": "April"
}];
var b = ["June", "May"];
var healthTemp = [];
a.forEach(item => {
var idx = b.value.indexOf(item);
console.log(idx);
if (idx == 0) healthTemp.add('Previous month')
if (idx == 1) healthTemp.add('2 months ago')
if (idx == 2) healthTemp.add('3 months ago')
});
Here since there is June and May in b
are in indexes 0
, 1
, I want healthTemp
to be ['Previous month', '2 months ago']
. But this gives me an error. Please let me know where am I going wrong and how can I fix this?
Thanks Chris for the suggestion, I've updated my question by replacing =
with ==
. Now I get the error as Uncaught TypeError: Cannot read property 'indexOf' of undefined"
.