我只想将文本附加到对象
I just want to append text to an object
现在,对于每个包含字符串“shirt”的商品,我想将 shirt 替换为“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: "",},
];
我希望它看起来像这样
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: "",},
];
我使用了地图功能,但它不包括未修改的行
我的代码
const test = convertedToJson.map((convertedToJson) => {
if (!!convertedToJson.item.match(/Shirt/i)) {
return convertedToJson.item + "Shirts are not available ";
}
});
console.log(test);
“我的输出”
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;
})
您可以使用映射和替换
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: "",
},
];
const newVal = inventory.map((elem) => {
// checking if string contains shirt/Shirt
if (elem.item.toLowerCase().indexOf('shirt') !== -1) {
return {
...elem,
// case-insensitive replacement
item: elem.item.replace(/shirt/gi, "Shirts are not available")
}
} else {
return { ...elem
}
}
});
console.log(newVal)
const test = convertedToJson.map((convertedToJson) => {
if (convertedToJson.item.toLowerCase().indexOf('shirt') !== -1) {
convertedToJson.item = convertedToJson.item.replace(/shirt/gi, "Shirts are not available");
}
return convertedToJson
});
console.log(test);
您的代码存在的问题是,只有在满足条件的情况下,您才 return 计算值。我只是 return 外面的值,如果 'if condition' 得到满足,该值将被操纵
这边...
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: "" }
]
inventory.forEach((obj,i,arr)=>
arr[i].item = obj.item.replace(/shirt/i, 'Shirts are not available'))
console.log( inventory )
.as-console-wrapper {max-height: 100%!important;top:0;}
您可以使用 Array.prototype.map
轻松实现此目的。
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: ""
},
];
const result = inventory.map((el) => {
if (el.item.toLowerCase().includes("shirt")) {
return { ...el,
item: "Shirts are not available small"
};
}
return el;
});
console.log( result );
现在,对于每个包含字符串“shirt”的商品,我想将 shirt 替换为“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: "",},
];
我希望它看起来像这样
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: "",},
];
我使用了地图功能,但它不包括未修改的行
我的代码
const test = convertedToJson.map((convertedToJson) => {
if (!!convertedToJson.item.match(/Shirt/i)) {
return convertedToJson.item + "Shirts are not available ";
}
});
console.log(test);
“我的输出”
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;
})
您可以使用映射和替换
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: "",
},
];
const newVal = inventory.map((elem) => {
// checking if string contains shirt/Shirt
if (elem.item.toLowerCase().indexOf('shirt') !== -1) {
return {
...elem,
// case-insensitive replacement
item: elem.item.replace(/shirt/gi, "Shirts are not available")
}
} else {
return { ...elem
}
}
});
console.log(newVal)
const test = convertedToJson.map((convertedToJson) => {
if (convertedToJson.item.toLowerCase().indexOf('shirt') !== -1) {
convertedToJson.item = convertedToJson.item.replace(/shirt/gi, "Shirts are not available");
}
return convertedToJson
});
console.log(test);
您的代码存在的问题是,只有在满足条件的情况下,您才 return 计算值。我只是 return 外面的值,如果 'if condition' 得到满足,该值将被操纵
这边...
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: "" }
]
inventory.forEach((obj,i,arr)=>
arr[i].item = obj.item.replace(/shirt/i, 'Shirts are not available'))
console.log( inventory )
.as-console-wrapper {max-height: 100%!important;top:0;}
您可以使用 Array.prototype.map
轻松实现此目的。
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: ""
},
];
const result = inventory.map((el) => {
if (el.item.toLowerCase().includes("shirt")) {
return { ...el,
item: "Shirts are not available small"
};
}
return el;
});
console.log( result );