0

ネストされたオブジェクトがあります。

ここで、オブジェクトに常に「配列」であるキーとして「items」が含まれているかどうかを確認し、「type」:「list」を親の「type」:「array」に置き換える必要があります。

第1レベルではうまく機能しますが、「アイテム」を含むネストされたオブジェクトになると、行き詰まります。

function convertData() {
 const arr = {
  "type": "list",
  "items": [{
   "type": "list",
   "items": [{
    "type": "string",
    "value": 0,
    "unit": "",
    "pattern": "^(auto|0)$|^[+-]?[0-9]+(\\.)?([0-9]+)?(rem|px|em|ex|%|in|cm|mm|pt|pc)$"
   }, {
    "type": "string",
    "value": 0.1875,
    "unit": "rem",
    "pattern": "^(auto|0)$|^[+-]?[0-9]+(\\.)?([0-9]+)?(rem|px|em|ex|%|in|cm|mm|pt|pc)$"
   }, {
    "type": "string",
    "value": 0.75,
    "unit": "rem",
    "pattern": "^(auto|0)$|^[+-]?[0-9]+(\\.)?([0-9]+)?(rem|px|em|ex|%|in|cm|mm|pt|pc)$"
   }, {
    "type": "string",
    "value": 0,
    "unit": "",
    "pattern": "^(auto|0)$|^[+-]?[0-9]+(\\.)?([0-9]+)?(rem|px|em|ex|%|in|cm|mm|pt|pc)$"
   }]
  }, {
   "type": "string",
   "value": {
    "r": 161,
    "g": 161,
    "b": 161,
    "a": 0.75,
    "hex": "#a1a1a1"
   },
   "pattern": "^rgba?\\(((25[0-5]|2[0-4]\\d|1\\d{1,2}|\\d\\d?)\\s*,\\s*?){2}(25[0-5]|2[0-4]\\d|1\\d{1,2}|\\d\\d?)\\s*,?\\s*([01]\\.?\\d*?)?\\)"
  }]
 };
 if (Array.isArray(arr.items)) {
  arr.type = "array";
  console.log(arr);
 }
}
<button onclick="convertData()">Click me!</button>

4

1 に答える 1