【VUE】vue宣告json物件自定義key,value值
阿新 • • 發佈:2021-01-23
技術標籤:總結
1、定義一個物件,和一個數組,如下
//定義一個物件 let jsonObject = {}; //定義一個數組 let json = [ { key: "keyword1", value: "value1", }, { key: "keyword2", value: "value2", }, { key: "keyword3", value: "value3", }, { key: "keyword4", value: "value4", }, { key: "keyword5", value: "value5", }, ];
2、使用$set()方法對json物件進行賦值
//使用for迴圈編輯陣列,向jsonObject中賦值
for (let itemindex = 0; itemindex < json.length; itemindex++) {
//$set()方法第一個引數是物件,第二個引數是key值,第三個引數是value值
this.$set(jsonObject, json[itemindex].key, json[itemindex].value);
}
3、列印輸出結果
console.log("顯示jsonObject"); console.log(jsonObject);
全部程式碼
let jsonObject = {}; let json = [ { key: "keyword1", value: "value1", }, { key: "keyword2", value: "value2", }, { key: "keyword3", value: "value3", }, { key: "keyword4", value: "value4", }, { key: "keyword5", value: "value5", }, ]; for (let itemindex = 0; itemindex < json.length; itemindex++) { this.$set(jsonObject, json[itemindex].key+"", json[itemindex].value+""); } console.log("顯示jsonObject"); console.log(jsonObject);