为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

一只名叫tom的猫 2022-06-09 10:58:01
我有一个如下指定的对象:{"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)      });}
查看完整描述

3 回答

?
蛊毒传说

TA贡献1895条经验 获得超3个赞

您可以使用for..in


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"},]}


for (let key in data) {

  data[key].forEach(obj => {

    if (obj.dataType === "DD") {

      obj.selectedData = +(obj.selectedData || 0)

    }

  })

}


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) {

  newObj[key] = data[key]

  data[key].forEach(obj => {

    if (obj.dataType === "DD") {

      newObj.selectedData = +(obj.selectedData || 0)

    }

  })

}


console.log(newObj)


查看完整回答
反对 回复 2022-06-09
?
临摹微笑

TA贡献1982条经验 获得超2个赞

我们可以在主 obj 上使用过滤器,然后继续修改对象。


    function modifyDataToNumber(){

        let myObject = jsonObj['player settings'];

        let ddMyObject = myObject.filter((row)=>(row["dataType"]==="DD"));

        console.log(ddMyObject[0]["selectedData"]);

        ddMyObject.forEach((row,index)=>{

            ddMyObject[index]["selectedData"] = +ddMyObject[index]["selectedData"];

        })


        console.log(jsonObj);

    }

    modifyDataToNumber();


查看完整回答
反对 回复 2022-06-09
?
慕标琳琳

TA贡献1830条经验 获得超9个赞

既然您说“重新生成”,我假设您想要一种不可变的方法(即,生成具有所需更改的数据副本,而不是更改原始对象)。


为此,您可以使用扩展语法和Array#map:


let convertSetting = setting => ({

    ...setting,

    selectedData: setting.dataType === "DD"

        ? parseInt(setting.selectedData)

        : setting.selectedData

});


let convert = x => ({

    ...x,

    ["player settings"]: x["player settings"].map(convertSetting)

});

然后,您可以将该功能用作convert(yourOriginalObject).


查看完整回答
反对 回复 2022-06-09
  • 3 回答
  • 0 关注
  • 152 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信