亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

搜索并替換標題鍵嵌套對象vue js上的字符串

搜索并替換標題鍵嵌套對象vue js上的字符串

長風秋雁 2023-09-21 16:51:56
我有一個嵌套對象,其中包含幾個鍵,這些鍵還包括標題和子項。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: [{...}]}
查看完整描述

1 回答

?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

您可以嘗試使用遞歸策略來查找數組中的任何鍵,并搜索它們的子項。


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)


查看完整回答
反對 回復 2023-09-21
  • 1 回答
  • 0 關注
  • 108 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號