Javascript 在对象数组中查找并替换部分属性值

Javascript 在对象数组中查找并替换部分属性值,javascript,Javascript,我需要在一组对象上搜索关键字,并替换它的所有实例 例如,我有以下数组: const test = [ { marketType: 90, displayName: "FT Total Match {scoreType} Over / Under 0.75 Remove", }, { marketType: 90, displayName: "FT Total Match {scoreType} Over / Under 1 Remove", }, ] 我想在

我需要在一组对象上搜索关键字,并替换它的所有实例

例如,我有以下数组:

const test = [
  {
    marketType: 90,
    displayName: "FT Total Match {scoreType} Over / Under 0.75 Remove",
},
{
    marketType: 90,
    displayName: "FT Total Match {scoreType} Over / Under 1 Remove",
},
]
我想在上面的数组中查找所有
{scoreType}
并将其替换为
目标

到目前为止,我已尝试将数组转换为字符串,对其运行替换,然后将其转换回数组。但是当我控制台记录结果时,我仍然看到
{scoreType}
并且没有错误

console.log('result: ', JSON.parse(JSON.stringify(test).replace('{scoreType}', 'goals')));
有人能告诉我我做错了什么吗?

用于像这样迭代和替换displayName

var updatedTest = test.map(obj => ({...obj, displayName: obj.displayName.replace('{scoreType}', 'goals')}));
用于像这样迭代和替换displayName

var updatedTest = test.map(obj => ({...obj, displayName: obj.displayName.replace('{scoreType}', 'goals')}));

只需使用
map

const result = test.map(item => ({
  ...item,
  displayName: item.displayName.replace('{scoreType}', 'goals'),
}))

只需使用
map

const result = test.map(item => ({
  ...item,
  displayName: item.displayName.replace('{scoreType}', 'goals'),
}))

要修复原始代码,必须使用全局正则表达式
replace

const测试=[
{
市场类型:90,
displayName:“0.75以上/以下的FT总匹配{scoreType}删除”,
},
{
市场类型:90,
displayName:“在1上方/下方移除FT总匹配{scoreType}”,
},
]

log('result:',JSON.parse(JSON.stringify(test).replace(/{scoreType}/g,'goals'))若要修复原始代码,必须使用全局正则表达式
replace

const测试=[
{
市场类型:90,
displayName:“0.75以上/以下的FT总匹配{scoreType}删除”,
},
{
市场类型:90,
displayName:“在1上方/下方移除FT总匹配{scoreType}”,
},
]
log('result:',JSON.parse(JSON.stringify(test).replace(/{scoreType}/g,'goals'))您可以使用并执行以下操作:

const测试=[
{
市场类型:90,
displayName:“0.75以上/以下的FT总匹配{scoreType}删除”,
},
{
市场类型:90,
displayName:“在1上方/下方移除FT总匹配{scoreType}”,
},
]
newTest=test.map(obj=>({…obj,displayName:obj.displayName.replace({scoreType}',goals}))
console.log(newTest)您可以使用并执行以下操作:

const测试=[
{
市场类型:90,
displayName:“0.75以上/以下的FT总匹配{scoreType}删除”,
},
{
市场类型:90,
displayName:“在1上方/下方移除FT总匹配{scoreType}”,
},
]
newTest=test.map(obj=>({…obj,displayName:obj.displayName.replace({scoreType}',goals}))

console.log(newTest)将对象转换为字符串,然后处理它是一种非常模糊的方法,可能会导致不希望出现的错误

您可以使用
array#forEach
在数组上循环,并使用从源字符串生成的正则表达式替换
displayName
的文本

const测试=[{
市场类型:90,
displayName:“0.75以上/以下的FT总匹配{scoreType}删除”,
},
{
市场类型:90,
displayName:“在1上方/下方移除FT总匹配{scoreType}”,
},
];
常量搜索='scoreType';
常量替换=‘目标’;
test.forEach(项=>{
const regex=new RegExp(`{${search}\}`,'g')
item.displayName=item.displayName.replace(regex,replacement);
});

控制台日志(测试)将对象转换为字符串,然后处理它是一种非常模糊的方法,可能会导致不希望出现的错误

您可以使用
array#forEach
在数组上循环,并使用从源字符串生成的正则表达式替换
displayName
的文本

const测试=[{
市场类型:90,
displayName:“0.75以上/以下的FT总匹配{scoreType}删除”,
},
{
市场类型:90,
displayName:“在1上方/下方移除FT总匹配{scoreType}”,
},
];
常量搜索='scoreType';
常量替换=‘目标’;
test.forEach(项=>{
const regex=new RegExp(`{${search}\}`,'g')
item.displayName=item.displayName.replace(regex,replacement);
});
控制台日志(测试)