JavaScript /打字稿:无法重新生成对象

时间:2020-04-01 05:12:42

标签: 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"
    }
  ]
}

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

  1. 如果dataType ==='DD',则将selectedData转换为数字。

我编写了以下代码,但卡在了这里:

for (var j = 0; j < this.myobject.length; j++){
      this.myobject.forEach(obj => {
        console.log(obj)
      });
}

4 个答案:

答案 0 :(得分: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)

答案 1 :(得分:1)

我们可以在主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();

答案 2 :(得分:0)

我会做这样的事情

const json = {
  "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"
    },

  ]
};


json['player settings'] = json['player settings'].map(setting => {
  if (setting.dataType === 'DD') {
    const updatedSetting = {
      ...setting,
      selectedData: parseInt(setting.selectedData)
    };
    
    return updatedSetting;
  }
  
  return setting;
});

console.log('Result', json);

答案 3 :(得分:0)

由于您说的是“重新生成”,因此我假设您希望对此采取一种不变的方法(即,生成具有所需更改的数据副本,而不是更改原始对象)。

为此,您可以使用spread syntaxArray#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)