test.html 1.01 KB
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>

    function replaceAttrPlaceholder(oldValue = {}, replaceAttr = '', replaceValue = '', newValue = {},) {
      if (typeof oldValue !== 'object') return newValue

      for (const key of Object.keys(oldValue)) {
        if (key === 'attrPlaceholder') {
          newValue[replaceAttr] = replaceValue
          continue
        }
        if (typeof oldValue[key] === 'object') {
          newValue[key] = replaceAttrPlaceholder(oldValue[key], replaceAttr, replaceValue)
        }
      }

      return newValue
    }

    const value = {
      attrPlaceholder: {
        a: 123
      },
      name: {
        attrPlaceholder: 123,
        b: {
          attrPlaceholder: 123
        }
      }
    }

    console.log(replaceAttrPlaceholder(value, 'co', 2))
  </script>
</body>

</html>