第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何構建用于動態(tài)查找值的 JSON 鍵

如何構建用于動態(tài)查找值的 JSON 鍵

青春有我 2022-12-22 14:48:23
背景我正在創(chuàng)建一個引擎來從different JSON文件中獲取值,每個文件都帶有different structure. 我正在尋找進入store the key as string (or array)靜態(tài)文件的方法,并使用鍵來獲取值。要存儲在靜態(tài)文件中的字符串或數(shù)組形式的鍵可能看起來像hello.world.that.is.somethingandthat.is.something.different或在數(shù)組["hello", "world", "that", "is", "something"]and中["that", "is", "something", "different"]。鑒于下面的示例數(shù)據(jù),有什么方法可以從預構建密鑰(存儲在某處)中檢索值[1, 2, 3, 4, 5]和值?Noo!!!樣本數(shù)據(jù)let data = {  hello: {    world: {      that: {        is: {          something: [1, 2, 3, 4, 5]        }      }    }  },  that: {    is: {      something: {        different: "Noo!!!"      }    }  }}預期數(shù)據(jù)data[pre_build_keys_1] // [1, 2, 3, 4, 5]data[pre_build_keys_2] // Noo!!!
查看完整描述

4 回答

?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

您可以使用eval()它評估字符串并將其視為節(jié)點/變量,假設您已經聲明了一個與評估的字符串等效的變量名稱。


let data = {

  hello: {

    world: {

      that: {

        is: {

          something: [1, 2, 3, 4, 5]

        }

      }

    }

  },

  that: {

    is: {

      something: {

        different: "Noo!!!"

      }

    }

  }

}


let pre_build_keys_1 = "data.hello.world.that.is.something"

let pre_build_keys_2 = "data.that.is.something.different"


console.log(eval(pre_build_keys_1))

console.log(eval(pre_build_keys_2))


查看完整回答
反對 回復 2022-12-22
?
梵蒂岡之花

TA貢獻1900條經驗 獲得超5個贊

假設數(shù)據(jù)結構穩(wěn)定且與示例一致,我認為您已經完成了所有艱苦的工作!在這一點上,您可以評估您想要的確切路徑并返回它。


console.log(eval("data.hello.world.that.is.something"));

console.log(eval("data.that.is.something.different"));


查看完整回答
反對 回復 2022-12-22
?
RISEBY

TA貢獻1856條經驗 獲得超5個贊

您可以像下面這樣創(chuàng)建函數(shù)并使用數(shù)組getData傳遞data對象。單擊此處了解更多關于used in 的信息。keyreducegetData


function getData(data, keys) {

  return keys.reduce((acc, key) => acc[key], data);

}


let pre_build_keys_1 = ["hello", "world", "that", "is", "something"];

let pre_build_keys_2 = ["that", "is", "something", "different"]


let data = {

  hello: {

    world: {

      that: {

        is: {

          something: [1, 2, 3, 4, 5]

        }

      }

    }

  },

  that: {

    is: {

      something: {

        different: "Noo!!!"

      }

    }

  }

};


console.log(getData(data, pre_build_keys_1)); // [1, 2, 3, 4, 5]

console.log(getData(data, pre_build_keys_2)); // Noo!!!


查看完整回答
反對 回復 2022-12-22
?
開心每一天1111

TA貢獻1836條經驗 獲得超13個贊

你可以創(chuàng)建一個代理對象來處理從對象中獲取適當?shù)捻椖浚缓竽憧梢詮淖置嫔献鰀ata['hello.world.that.is.something']來獲取你想要的東西,就像這樣:


let real_data = {

  hello: {

    world: {

      that: {

        is: {

          something: [1, 2, 3, 4, 5]

        }

      }

    }

  },

  that: {

    is: {

      something: {

        different: "Noo!!!"

      }

    }

  }

}


const handler = {

  get: function(target, prop, receiver) {

    let parsed;

    try {

        parsed = JSON.parse(prop);

    } catch (e) {

      if (prop.startsWith('[')) {

          parsed = prop.substring(1, prop.length - 1);

          parsed = parsed.split(', ').join(',').split(',');

      } else {

          parsed = prop.split('.');

      }

    }

    return parsed.reduce((carry, current) => carry[current], target);

  }

};


const data = new Proxy(real_data, handler);



console.log(data['hello.world.that.is.something'])

console.log(data['[that, is, something, different]'])

console.log(data['["that", "is", "something", "different"]'])


查看完整回答
反對 回復 2022-12-22
  • 4 回答
  • 0 關注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

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

公眾號

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