我需要在对象数组中搜索关键字并替换它的所有实例。
例如,我有以下数组:
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}
并将其替换为 goals
。
到目前为止,我已尝试将数组转换为字符串,对其运行替换,然后将其转换回数组。但是当我控制台记录结果时,我仍然看到 {scoreType}
并且没有错误。
console.log('result: ', JSON.parse(JSON.stringify(test).replace('{scoreType}', 'goals')));
谁能告诉我我做错了什么?
最佳答案
试试 map
:
const result = test.map(item => ({
...item,
displayName: item.displayName.replace('{scoreType}', 'goals'),
}))
关于javascript - 在对象数组中查找并替换部分属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51667175/