I am trying to compare the values from 2 state arrays, but when I look at the value of otherItems[i] I get undefined. For clarification, Items is an array of Objects with many data points, why I'm calling it with .name, while otherItems is just an array of the names. If I call just this.state.otherItems then I get the full array contents.
this.state = {
Items: [],
otherItems: [],
...
}
...
function = () => {
var total = this.state.total;
var i;
for(i = 0; i < this.state.Items.length; i++) {
console.log(this.state.otherItems[i]);
if(this.state.Items[i].name === this.state.otherItems[i]) {
total += parseFloat(this.state.Items[i].price)
}
}
this.setState({Total: total})
}
What I would like to happen is the items from the two arrays compare, and the price of the item is added to the total sum if and only if the item i from Items exists in otherItems.
basketItems[i], nototherItems[i]. Is that what you mean gives youundefined?console.log(this.state.otehrItems[i]);it should beconsole.log(this.state.otherItems[i]);