1

I'm trying to use map method use instead of for loop using React Typescript for below code but not working

Actual Code using for loop :

renderFunction(perkProgressData) {
    let tiers = [];
    let currentData = { item: {} }
    const data = perkProgressData.perkProgressData
    if (data && data.program && data.program.tiers) {
      tiers = data.program.tiers
      **for (let i = 0; i < tiers.length; i++) {**
        if (tiers[i].tierPerks[0].perkStatus === "UNCLAIMED" ||
          data.currentTierId === tiers[i].tierId) {
          currentData = { item: tiers[i] }
          break;
        }
      }
    }

tried using .map():(Not Working )

renderFunction(perkProgressData) {
    let tiers = [];
    let currentData = { item: {} }
    const data = perkProgressData.perkProgressData
    if (data && data.program && data.program.tiers) {
      tiers = data.program.tiers
      tiers.map((val) => {
        if (val.tierPerks && val.tierPerks[0].perkStatus === "UNCLAIMED" || 
          data.currentTierId === val.tierId) {
            currentData = { item: val }
          }
        });
      }
    }
5
  • There's no reason to use map in that code. You don't need the new array it creates. Commented Nov 13, 2019 at 16:33
  • MAP returns a new array but for loop doesn't. Do you need any a new array? Commented Nov 13, 2019 at 16:34
  • 1
    if for some reason you don't want to use for loop, you could use a forEach, but not map for sure Commented Nov 13, 2019 at 16:37
  • Yes, is better to use forEach in that case Commented Nov 13, 2019 at 16:41
  • 1
    @Konstantin - Well, the OP wants to break the loop early (at least, the code they're replacing does), so it would be more of a some thing. But really, what they're doing is finding something in the array, so find. But it's troubling, I've seen a lot of this "map as a general way to loop through arrays" misunderstanding the last 18-24 months. Someone must be mis-teaching this. Commented Nov 13, 2019 at 16:42

1 Answer 1

2

map isn't the right choice here, you don't need the new array it creates. Also, you want to stop early, which map doesn't do (unless you throw an error).

The for loop is fine, although you might consider for-of instead since you don't use the index:

renderFunction(perkProgressData) {
    let tiers = [];
    let currentData = { item: {} };
    const data = perkProgressData.perkProgressData;
    if (data && data.program && data.program.tiers) {
      tiers = data.program.tiers;
      for (const tier of tiers) {
        if (tier.tierPerks[0].perkStatus === "UNCLAIMED" ||
          data.currentTierId === tier.tierId) {
          currentData = { item: tier };
          break;
        }
      }
    }
    // ...

Or if you don't need tiers after the loop:

renderFunction(perkProgressData) {
    let currentData = { item: {} };
    const data = perkProgressData.perkProgressData;
    if (data && data.program && data.program.tiers) {
      for (const tier of data.program.tiers) {
        if (tier.tierPerks[0].perkStatus === "UNCLAIMED" ||
          data.currentTierId === tier.tierId) {
          currentData = { item: tier };
          break;
        }
      }
    }
    // ...

If you really want to use an array method, find would make sense:

renderFunction(perkProgressData) {
    const data = perkProgressData.perkProgressData
    let currentData =
      ( data &&
        data.program &&
        data.program.tiers &&
        data.program.tiers.find(tier =>
          tier.tierPerks[0].perkStatus === "UNCLAIMED" || data.currentTierId === tier.tierId
        )
      ) ||  { item: {} };
    // ...
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for helping me T.J. Crowder ,Its working

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.