javascript - 在标题键嵌套对象vue js上搜索和替换字符串
问题描述
我有一个包含多个键的嵌套对象,这些键还包括标题和子项。children 也是具有标题和子键的对象数组。(它们看起来像一棵树)如何搜索和替换一个单词或标题值的一部分?
const object = {
id: 'uuid',
title: 'hello You',
type: number,
visibility: true,
children: [
{
id: 'uuid',
title: 'You don't have any comments...',
type: number,
visibility: true,
children: [{...}, {...}, ...],
key1: {...},
key2: [{...}]
},
{
id: 'uuid',
title: 'You your problem is not with json...',
type: number,
visibility: true,
children: [{...}, {...}, ...],
key1: {...},
key2: [{...}]
}
],
key1: {...},
key2: [{...}]
}
搜索you
和替换world
标题
output = {
id: 'uuid',
title: 'hello world',
type: number,
visibility: true,
children: [
{
id: 'uuid',
title: 'world don't have any comments...',
type: number,
visibility: true,
children: [{...}, {...}, ...],
key1: {...},
key2: [{...}]
},
{
id: 'uuid',
title: 'world your problem is not with json...',
type: number,
visibility: true,
children: [{...}, {...}, ...],
key1: {...},
key2: [{...}]
}
],
key1: {...},
key2: [{...}]
}
解决方案
您可以尝试使用递归策略来查找数组中的任何键,也可以搜索它们的孩子。
function recursive (newArray) {
newArray.map(obj => {
if (obj.title) {
obj.title = obj.title.replace(/You/g, 'world')
}
if (obj.children) {
return recursive (obj.children)
}
})
return newArray
}
使用功能
let arr = [object]
recursive(arr)
推荐阅读
- java - 安装 Java 时出现 Swingbench 错误
- python - 如何删除字段包含无的最后一行?
- autodesk-forge - | 欧特克锻造 | 将 RVT 导出到 NWF、NWD、NWC
- ios - UIPageViewController - 检测到中途滚动到下一个视图控制器(几乎可以工作)以更改按钮颜色?
- mysql - 如何在另一列 MySQL 中标记重复值
- spring-integration - 测试 Spring 集成流程
- javascript - CSV 值转换为 span 元素
- python - 需要一些关于请求和 wrike api 的信息
- c# - 如何在 sharepoint CSOM 上获取审核日志
- ios - 美国的搜索区域无效