JavaScript / typescript:无法重新生成对象

我有一个如下指定的对象:


{

"player settings": [

    {

      "id": 1,

      "labelName": "site language",

      "labelValue": [

        {

          "id": 1,

          "languageName": "ARABIC",

          "language": "لغتك",

          "languageCode": "AE"

        },

        {

          "id": 2,

          "languageName": "CHINESE",

          "language": "你的语言",

          "languageCode": "ZH"

        },

      ],

      "dataType": "DD",

      "selectedData": "2"

    },

    {

      "id": 2,

      "labelName": "subtitle language",

      "labelValue": [

        {

          "id": 1,

          "languageName": "ARABIC",

          "language": "لغتك",

          "languageCode": "AE"

        },

        {

          "id": 2,

          "languageName": "CHINESE",

          "language": "你的语言",

          "languageCode": "ZH"

        },


      ],

      "dataType": "DD",

      "selectedData": "1"

    },


  ]

},

{

  "channel": [

    {

      "id": 11,

      "labelName": "channel label",

      "dataType": "TX",

      "selectedData": "jhfh"

    }

  ]

},

{

  "others": [

    {

      "id": 16,

      "labelName": "others label",

      "dataType": "TX",

      "selectedData": "dhgdhg"

    }

  ]

}

如何在以下条件下修改和重新生成对象:


if dataType=== 'DD' 然后转换selectedData成数字。

我写了下面的代码,但卡在这里:


for (var j = 0; j < this.myobject.length; j++){

      this.myobject.forEach(obj => {

        console.log(obj)

      });

}


一只名叫tom的猫
浏览 153回答 3
3回答

蛊毒传说

您可以使用for..inlet data = {"player settings": [{"id": 1,"labelName": "site language","labelValue": [{"id": 1,"languageName": "ARABIC","language": "لغتك","languageCode": "AE"},{"id": 2,"languageName": "CHINESE","language": "你的语言","languageCode":"ZH"},],"dataType": "DD","selectedData": "2"},],"player settings2": [{"id": 1,"labelName": "site language","labelValue": [{"id": 1,"languageName": "ARABIC","language": "لغتك","languageCode": "AE"},{"id": 2,"languageName": "CHINESE","language": "你的语言","languageCode":"ZH"},],"dataType": "NO DD","selectedData": "2"},]}for (let key in data) {&nbsp; data[key].forEach(obj => {&nbsp; &nbsp; if (obj.dataType === "DD") {&nbsp; &nbsp; &nbsp; obj.selectedData = +(obj.selectedData || 0)&nbsp; &nbsp; }&nbsp; })}console.log(data)不变的方法let data = {"player settings": [{"id": 1,"labelName": "site language","labelValue": [{"id": 1,"languageName": "ARABIC","language": "لغتك","languageCode": "AE"},{"id": 2,"languageName": "CHINESE","language": "你的语言","languageCode":"ZH"},],"dataType": "DD","selectedData": "2"},],"player settings2": [{"id": 1,"labelName": "site language","labelValue": [{"id": 1,"languageName": "ARABIC","language": "لغتك","languageCode": "AE"},{"id": 2,"languageName": "CHINESE","language": "你的语言","languageCode":"ZH"},],"dataType": "NO DD","selectedData": "2"},]}let newObj = {}for (let key in data) {&nbsp; newObj[key] = data[key]&nbsp; data[key].forEach(obj => {&nbsp; &nbsp; if (obj.dataType === "DD") {&nbsp; &nbsp; &nbsp; newObj.selectedData = +(obj.selectedData || 0)&nbsp; &nbsp; }&nbsp; })}console.log(newObj)

临摹微笑

我们可以在主 obj 上使用过滤器,然后继续修改对象。&nbsp; &nbsp; function modifyDataToNumber(){&nbsp; &nbsp; &nbsp; &nbsp; let myObject = jsonObj['player settings'];&nbsp; &nbsp; &nbsp; &nbsp; let ddMyObject = myObject.filter((row)=>(row["dataType"]==="DD"));&nbsp; &nbsp; &nbsp; &nbsp; console.log(ddMyObject[0]["selectedData"]);&nbsp; &nbsp; &nbsp; &nbsp; ddMyObject.forEach((row,index)=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ddMyObject[index]["selectedData"] = +ddMyObject[index]["selectedData"];&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; console.log(jsonObj);&nbsp; &nbsp; }&nbsp; &nbsp; modifyDataToNumber();

慕标琳琳

既然您说“重新生成”,我假设您想要一种不可变的方法(即,生成具有所需更改的数据副本,而不是更改原始对象)。为此,您可以使用扩展语法和Array#map:let convertSetting = setting => ({&nbsp; &nbsp; ...setting,&nbsp; &nbsp; selectedData: setting.dataType === "DD"&nbsp; &nbsp; &nbsp; &nbsp; ? parseInt(setting.selectedData)&nbsp; &nbsp; &nbsp; &nbsp; : setting.selectedData});let convert = x => ({&nbsp; &nbsp; ...x,&nbsp; &nbsp; ["player settings"]: x["player settings"].map(convertSetting)});然后,您可以将该功能用作convert(yourOriginalObject).
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript