I want to count the number of entries based on the color inputted by user , like if user input red , the program should return 11. This is what I tried. The variable cnt is not defined after the loop.
let description=[
{color:'red',qty:6,remarks:'asdf'},
{color:'green',qty:5,remarks:'asdf'},
{color:'red',qty:5,remarks:'asdf'},
{color:'yellow',qty:5,remarks:'asdf'},
{color:'blue',qty:5,remarks:'asdf'},
{color:'white',qty:5,remarks:'asdf'}
];
{description.map((t,index) =>{
let cnt=0;
if(t.color=='red'){
cnt=cnt+parseInt(t.qty);
}
console.log(cnt);
}
)}
console.log(cnt);
letis scope specific. It's going to be re-created on each iteration and won't be defined once you leave it. Instead move it outside of your function and it will work.