0

I want to modify and re-generate an array of objects. Please check my created stackblitz.com

Please see the data dump:

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

How can I modify and re-generate the object with the following conditions:

if dataType === 'DD' then convert selectedData into number.

I wrote the below code but stuck here and got error ERROR Error: this.data[key].forEach is not a function:

for (let key in this.data) {
    this.data[key].forEach(obj => {
      if (obj.dataType === "DD") {
        obj.selectedData = +(obj.selectedData || 0)
      }
    });
 }

2 Answers 2

2

Each element of the array is an object. And object doesn't have a forEach() method. You could try the following function

ngOnInit() {
  this.data.forEach(item => {
    for (const key in item) {   // <-- `item.forEach()` won't work because it's an object
      item[key].forEach(subItem => {
        if (subItem.dataType === 'DD') {
          subItem.selectedData = +(subItem.selectedData || 0);
        }
      })
    }
  });
  console.log(this.data);
}

I've modified your Stackblitz

Sign up to request clarification or add additional context in comments.

Comments

1

Try this

this.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" }, { "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" } ] } ];

for (let key in this.data) {
    Object.values(this.data[key])[0].forEach(obj => {
      if (obj.dataType === "DD") {
        obj.selectedData = +(obj.selectedData || 0)
      }
    });
 }
 
 console.log(this.data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.