Home > Software engineering >  Replacing strings over array. Why one thing works while other similar code doesn't
Replacing strings over array. Why one thing works while other similar code doesn't

Time:06-25

I have two sets of code, one works the other doesn't. I can't for the life of me figure out why.

const arr1 = [
  { sku: '003CON', price: undefined },
  { sku: 1000, price: 2215.49 },
  { sku: 1001, price: 2215.49 },
  { sku: 'PD-1000B', price: undefined },
  { sku: 'PD-1007A', price: 2195.14 },
  { sku: 'PD-1042A', price: 2495.29 },
  { sku: 'BD111-50', price: 2495.29 },
  { sku: 'BD222-50', price: 2495.29 },
  { sku: 'BD333', price: 2495.29 },
  { sku: 'KL.213', price: 2495.29 },
  { sku: 'KL.312', price: undefined },
  { sku: 1005, price: 2495.29 },
  { sku: 5343, price: undefined },
  { sku: 1231, price: undefined },
  { sku: 3542, price: 2495.29 },
  { sku: 6756, price: 2495.29 },
  { sku: 7685, price: undefined },
  { sku: 1076, price: 2495.29 },
] 

var what = arr1.map((e) => {
  if (typeof e.sku === 'string') {
    return {
      sku: e.sku.startsWith("PD-") ? e.sku.replace("S/A", "").trim() :
        e.sku.startsWith('KL') ? e.sku.replace('.', '').trim() :
        e.sku.startsWith('BD') ? e.sku.replace('-50', '').trim() :
        e.sku,
      price: typeof e.price === "undefined" ? "0" : e.price,
    };
  }
  else {
    return {
      sku: e.sku,
      price: typeof e.price === 'undefined' ? '0' : e.price
    }
  }
});

what.forEach(e => {
  if (typeof e.sku === 'string') {
    if (e.sku.includes('PD-1000B')) {
      e.sku = 'PD-1000-B'
    }
    if (e.sku.includes('PD-1007A')) {
      e.sku = 'PD-1007-A'
    }
    if (e.sku.includes('PD-1042A')) {
      e.sku = 'PD-1042-A'
    }
  }
})

console.log(what)

var what2 = arr1.map((e) => {
  if (typeof e.sku === 'string') {
    return {
      sku: e.sku.startsWith("PD-") ? e.sku.replace("S/A", "").trim() :
        e.sku.startsWith('KL') ? e.sku.replace('.', '').trim() :
        e.sku.startsWith('BD') ? e.sku.replace('-50', '').trim() :
        e.sku.startsWith('PD-1000') ? e.sku.replace('PD-1000','PD-1000-') : //this doesnt work
        e.sku.startsWith('PD-1007') ? e.sku.replace('PD-1007','PD-1007-') : //this
        e.sku.startsWith('PD-1042') ? e.sku.replace('PD-1042','PD-1042-') : //this
        e.sku,
      price: typeof e.price === "undefined" ? "0" : e.price,
    };
  }
  else {
    return {
      sku: e.sku,
      price: typeof e.price === 'undefined' ? '0' : e.price
    }
  }
});

console.log(what2)

So I """have to""" do the forEach or else the 'PD-1000B' et al don't get replaced ????

The only thing I can think of is that maybe if I already looped through startsWith 'PD-' I can't do something similar?

CodePudding user response:

Your issue is that in your massive ternary statement:

sku: e.sku.startsWith("PD-") ? e.sku.replace("S/A", "").trim() :
    e.sku.startsWith('KL') ? e.sku.replace('.', '').trim() :
    e.sku.startsWith('BD') ? e.sku.replace('-50', '').trim() :
    e.sku.startsWith('PD-1000') ? e.sku.replace('PD-1000','PD-1000-') : //this doesnt work
    e.sku.startsWith('PD-1007') ? e.sku.replace('PD-1007','PD-1007-') : //this
    e.sku.startsWith('PD-1042') ? e.sku.replace('PD-1042','PD-1042-') : //this
    e.sku,

any sku that starts with PD- will match the first expression and so will never be tested against PD-1000 etc. It's easier to process the other conditions first e.g.:

sku = e.sku.startsWith('PD-1007') ? e.sku.replace('PD-1007','PD-1007-') :
    e.sku.startsWith('PD-1042') ? e.sku.replace('PD-1042','PD-1042-') :
    e.sku.startsWith('PD-1000') ? e.sku.replace('PD-1000','PD-1000-') :
    e.sku
return {
  sku: sku.startsWith("PD-") ? sku.replace("S/A", "").trim() :
    sku.startsWith('KL') ? sku.replace('.', '').trim() :
    sku.startsWith('BD') ? sku.replace('-50', '').trim() :
    sku,
  price: typeof e.price === "undefined" ? "0" : e.price,
};

Note there are other improvements you could make using regex e.g.

sku = e.sku.replace(/^(PD-\d{4})/, '$1-')

Working code:

const arr1 = [
  { sku: '003CON', price: undefined },
  { sku: 1000, price: 2215.49 },
  { sku: 1001, price: 2215.49 },
  { sku: 'PD-1000B', price: undefined },
  { sku: 'PD-1007A', price: 2195.14 },
  { sku: 'PD-1042A', price: 2495.29 },
  { sku: 'BD111-50', price: 2495.29 },
  { sku: 'BD222-50', price: 2495.29 },
  { sku: 'BD333', price: 2495.29 },
  { sku: 'KL.213', price: 2495.29 },
  { sku: 'KL.312', price: undefined },
  { sku: 1005, price: 2495.29 },
  { sku: 5343, price: undefined },
  { sku: 1231, price: undefined },
  { sku: 3542, price: 2495.29 },
  { sku: 6756, price: 2495.29 },
  { sku: 7685, price: undefined },
  { sku: 1076, price: 2495.29 },
] 

var what = arr1.map((e) => {
  if (typeof e.sku === 'string') {
    return {
      sku: e.sku.startsWith("PD-") ? e.sku.replace("S/A", "").trim() :
        e.sku.startsWith('KL') ? e.sku.replace('.', '').trim() :
        e.sku.startsWith('BD') ? e.sku.replace('-50', '').trim() :
        e.sku,
      price: typeof e.price === "undefined" ? "0" : e.price,
    };
  }
  else {
    return {
      sku: e.sku,
      price: typeof e.price === 'undefined' ? '0' : e.price
    }
  }
});

what.forEach(e => {
  if (typeof e.sku === 'string') {
    if (e.sku.includes('PD-1000B')) {
      e.sku = 'PD-1000-B'
    }
    if (e.sku.includes('PD-1007A')) {
      e.sku = 'PD-1007-A'
    }
    if (e.sku.includes('PD-1042A')) {
      e.sku = 'PD-1042-A'
    }
  }
})

console.log(what)

var what2 = arr1.map((e) => {
  if (typeof e.sku === 'string') {
    sku = e.sku.startsWith('PD-1007') ? e.sku.replace('PD-1007','PD-1007-') :
        e.sku.startsWith('PD-1042') ? e.sku.replace('PD-1042','PD-1042-') :
        e.sku.startsWith('PD-1000') ? e.sku.replace('PD-1000','PD-1000-') :
        e.sku
    return {
      sku: sku.startsWith("PD-") ? sku.replace("S/A", "").trim() :
        sku.startsWith('KL') ? sku.replace('.', '').trim() :
        sku.startsWith('BD') ? sku.replace('-50', '').trim() :
        sku,
      price: typeof e.price === "undefined" ? "0" : e.price,
    };
  }
  else {
    return {
      sku: e.sku,
      price: typeof e.price === 'undefined' ? '0' : e.price
    }
  }
});

console.log(what2)

  • Related