In a new complete series of me struggling with any concept:
I have two sets of code, one works the other doesn’t can’t figure out why for the life of me.
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 ????
I have no clue. Obviously I’m missing something. The only thing I can think of is that maybe if I already looped through startsWith ‘PD-‘ I can’t do something similar?
Thank you.

