1

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.

7
  • You are logging basketItems[i], not otherItems[i]. Is that what you mean gives you undefined? Commented Mar 10, 2019 at 21:27
  • 1
    @Tholle ah sorry that was a type, should be otherItems. I fixed it now. Commented Mar 10, 2019 at 21:29
  • You have a typo in console.log(this.state.otehrItems[i]); it should be console.log(this.state.otherItems[i]); Commented Mar 10, 2019 at 21:30
  • Alright. Could you clarify what it is you are trying to accomplish? Please provide a Minimal, Complete, and Verifiable example as it will make it a lot easier for someone to help you. Commented Mar 10, 2019 at 21:31
  • @NilsKähler Yes that was another typo I made when I made an edit. Commented Mar 10, 2019 at 21:31

1 Answer 1

2

You are using a loop variable i and using that to index both arrays, so you are only comparing elements with the same index between the two arrays, and the two arrays might differ in length.

You could instead use includes to check if the name of an element in Items is present in the otherItems array.

getTotal() {
  this.setState(({ total = 0, Items, otherItems }) => {
    Items.forEach(item => {
      if (this.state.otherItems.includes(item.name)) {
        total += parseFloat(item.price);
      }
    });

    return { otherTotal: total };
  });
}
Sign up to request clarification or add additional context in comments.

6 Comments

very good answer with prevState implemented to avoid async conflits
I tried this, but for some odd reason total returns as NaN or Not a Number.
@taway0282 Alright. Could you include all relevant code in your question? It might be that it's called Total instead of total, or something else that the current code doesn't show.
If there are any small inconsistencies, such as total instead of Total it's not the actual problem, just a transcription mistake. In the code editor it's the correct variable name.
This worked with minor tweaks, thank you very much :) Should I post the tweaked version as an edit or how should I go about this?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.