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

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

javascript - 如何“連接”屬性

javascript - 如何“連接”屬性

慕勒3428872 2022-07-08 10:00:34
我正在從 json 中檢索三個.js 場景的值?,F在我想將像“position.x:1”這樣的數據轉換為object[position][x]=1。對象 [ 鍵 ] = 值不起作用。顯然,它使它成為對象 [ position.x ] = 1。我將如何做到這一點或這種程序如何被稱為?
查看完整描述

2 回答

?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

沒有內置功能。


但是有很多第三方模塊可用于此任務。


例如,您可以使用lodash's _.set。文檔在這里,您也可以單獨使用它而無需添加完整lodash的包,而是使用lodash.set包。


例如,來自文檔:


var object = { 'a': [{ 'b': { 'c': 3 } }] };


_.set(object, 'a[0].b.c', 4);

console.log(object.a[0].b.c);

// => 4


_.set(object, ['x', '0', 'y', 'z'], 5);

console.log(object.x[0].y.z);

// => 5

如果你不喜歡,_.set還有很多 其他的 選擇 。


查看完整回答
反對 回復 2022-07-08
?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

你可以這樣做:


var string = "position.x : 1";

var split = string.split(/\.|\:/);   // split the strig into an array by '.' and ':'

var obj = {};                        // result object


function getObject(split) {

   obj[split[0].trim()] = {};

   obj[split[0].trim()][split[1].trim()] = split[2].trim();

}


getObject(split);                    // call the function and pass the array of values


console.log( obj );                  // print the whole result object

console.log( obj['position']['x'] ); // print obj['position']['x']


查看完整回答
反對 回復 2022-07-08
  • 2 回答
  • 0 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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