I have the following array, I would like to implement a function that will loop through the array, and replaces the names.val = '', except the second item or index[1]. I tried foreach function. but i don't know how to implement it properly
names = [
{
name: 'first',
inputType: 'number',
val: void 5
},
{
name: 'second',
inputType: 'number',
val: void 2
},
{
name: 'third',
inputType: 'text',
val: void 3
},
]
Tried the following
resetValues() {
let names = this.names
names.forEach((names:any, index:number)=>{
if(index=1){
names[1] = names[1].val
} else{
names = ''
}
})
}
index=1should beindex === 1, and you probably want to usenames.val. Reusing the same variable name over and over, shadowing the last, is quite confusing btw. I also don't understand, why you use a different number ever time forvoidindex===1, just donames.val = ''in case ofindex !== 1