Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
if statement returns false every time
function Carts() {
  let cartEval = [];
  const cartData = useSelector((state) => state.cartProducts);
  cartEval = cartData.map((item) => evalCart(item));
  function evalCart(a) {
    if (cartEval.find((b) => b.id == a.id)) {
      return { ...b, numOfTimes: numOfTimes + 1 };
    } else {
      return {
        id: a.id,
        numOfTimes: 1,
        prodTitle: a.title,
        price: a.price,
      };
    }
  }

  const totalPrice = cartData
    .map((item) => item.price)
    .reduce((x, y) => x + y, 0);
  function cartRender({ item }) {
    return (
      <View>
        <View>
          <View>
            <Text>{item.numOfTimes}</Text>
            <Text>{item.prodTitle}</Text>
            <Text>{item.price}</Text>
            <Text>Delete Button</Text>
          </View>
        </View>
      </View>
    );
  }

  return (
    <View>
      <View>
        <Text>Total Sum of Items:{totalPrice}</Text>
      </View>
      <FlatList data={cartEval} renderItem={cartRender} />
    </View>
  );
}

const styles = StyleSheet.create({});

export default Carts;

This is what I want to achieve:

I need the function evalCart(a) to check if the item.id exist in the cartEval. If the id exists, increase item.nuofItems by 1. If it does not, create a new item with the id in the cartEval array.

But every time the function is called it returns false, hence the first part of the if statement never executes. only the else part. What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I was finally able to solve the puzzle. This is my solution below

function Carts() {
  let cartEval = [];
  const cartData = useSelector((state) => state.cartProducts);
  cartData.map((item) => evalCart(item));
  function evalCart(a) {
    if (cartEval.find((b) => b.id == a.id)) {
      const dummy = cartEval.filter((d) => d.id == a.id)[0];
      cartEval.splice(cartEval.indexOf(dummy), 1, {
        ...dummy,
        numOfTimes: dummy.numOfTimes + 1,
      });
      
    } else {
      cartEval.push({
        id: a.id,
        numOfTimes: 1,
        prodTitle: a.title,
        price: a.price,
      });
    }
  }

  const totalPrice = cartData
    .map((item) => item.price)
    .reduce((x, y) => x + y, 0);
  function cartRender({ item }) {
    return (
      <View>
        <View>
          <View>
            <Text>{item.numOfTimes}</Text>
            <Text>{item.prodTitle}</Text>
            <Text>{item.price}</Text>
            <Text>Delete Button</Text>
          </View>
        </View>
      </View>
    );
  }

  return (
    <View>
      <View>
        <Text>Total Sum of Items:{totalPrice}</Text>
      </View>
      <FlatList data={cartEval} renderItem={cartRender} />
    </View>
  );
}

const styles = StyleSheet.create({});

export default Carts;

Thanks for your contributions

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!