Now for every item that has the string "shirt", I want to replace shirt to "Shirts are not available
const inventory = [
{ line_item_id: "55412", item: "Shirt small", description: "" },
{ line_item_id: "55342", item: "shirt big full", description: "" },
{ line_item_id: "1124", item: "Pant Small",description: "",},
];
I want it to look like this
const inventory = [
{ line_item_id: "55412", item: "Shirts are not available small", description: "" },
{ line_item_id: "55342", item: "Shirts are not available big full", description: "" },
{ line_item_id: "1124", item: "Pant Small",description: "",},
];
I have used the map function, but it does not including rows that weren’t modified
My code
const test = convertedToJson.map((convertedToJson) => {
if (!!convertedToJson.item.match(/Shirt/i)) {
return convertedToJson.item + "Shirts are not available ";
}
});
console.log(test);
"My output"
const inventory = [
'Shirts are not available small'
'Shirts are not available big full'
];
const convertedInventory = inventory.map((it) => {
const reg = new RegExp(/Shirt/i);
if (it.item.match(reg)) {
return {
...it,
item: it.item.replace(reg, 'Shirts are not available')
}
}
return it;
})