1

I'm very confused about doing foreach array to array object in Javascript, I already did a lot of research about foreach object in Javascript and I tried many ways but nothing works. All that I'm trying to achieve is to have data JSON like this :

[
            {
                "name": "First Data",
                "data": [
                    {
                        "y": 95,
                        "total":100,
                        "md": "1",
                        "name": "National",
                        "drillup" : 'level0',
                        "drilldown" : "3",
                        "next"  : "level2"
                    }

                ]
            }
            ,{
                "name": "Second Data",
                "data": [
                    {
                        "y": 95,
                        "total":100,
                        "md": "1",
                        "name": "National",
                        "drillup" : 'National',
                        "drilldown" : "3",
                        "next"  : "level2"
                    }

                ]
            }
        ]

and I tried to do foreach based on some finding of my research but the result wasn't like what I want or like what I'm try to achieve .. and here is the script that I tried :

  dataFirstSecond = await informationModel.getdata();
    Object.entries(dataRegularSecondary).forEach(entry => {
        const [key, value] = entry;
        returnData[key] = [
            {
                name: value.name,
                data: [{
                    y: value.y,
                    total: value.total_ada,
                    next: 'level_2',
                    drilldown: true,
                }]
            }]
    });

and here is the result or the output of my script that I try it :

{
    "0": [
        {
            "name": "First Data",
            "data": [
                {
                    "y": 22.973,
                    "total": 17,
                    "next": "level_2",
                    "drilldown": true
                }
            ]
        }
    ],
    "1": [
        {
            "name": "Second Data",
            "data": [
                {
                    "y": 5.4054,
                    "total": 4,
                    "next": "level_2",
                    "drilldown": true
                }
            ]
        }
    ]
}

can someone help me to achieve the data that I want?

6
  • 3
    returnData[key] = [{ ... }] should just be returnData.push({ ... }), and make sure returnData is an array (e.g. returnData = [] Commented Sep 14, 2021 at 9:34
  • Can you show us what the result should be ? Commented Sep 14, 2021 at 9:34
  • 1
    Neither dataRegularSecondary or resultData exist in your code. Can you try to provide a minimal reproducible example so we can debug it? Commented Sep 14, 2021 at 9:35
  • 1
    Is dataRegularSecondary really an object and not an array? Commented Sep 14, 2021 at 9:41
  • Where are you getting value.y from? Should it not be value.data[0].y? Or what does the data dataFirstSecond look like and, is it dataFirstSecond or dataRegularSecondary ? Commented Sep 14, 2021 at 9:56

4 Answers 4

1

returnData[key] = [{ ... }] should just be returnData.push({ ... }), and make sure returnData is an array (e.g. returnData = [])

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

Comments

0

If the function informationModel.getdata(); returns an Object you could use the method JSON.stringify(Object) to easily convert and Object to JSON. For example you could try to do to convert this Object to a String then cast the String to JSON.

let JSONString = JSON.stringify(informationModel.getdata());
let JSON_Object = JSON.parse(JSONString);

1 Comment

Thankyou for your answer, i found the best practice and check the comment from dave its work from gbu bro
0

If dataRegularSecondary is an array and not an object you could use map:

dataRegularSecondary.map(value => {
      return {
        name: value.name,
        data: [{
          y: value.y,
          total: value.total_ada,
          next: 'level_2',
          drilldown: true,
        }]
      }
    }

3 Comments

thank you for reply, can you suggest for this return change, include the variabel like php $data1[0] and $data1[2] dimenstional
Please clarify.
Thankyou for your answer, i found the best practice and check the comment from dave its work from gbu bro
0

Your question is how to forEach array to array object. Then that means dataRegularSecondary is an array, right? Object.entries returns an array of key value pairs. If you pass an array to that method, it will return the indices as keys and the items as values.

const arr = ['hello', 'world'];

Object.entries(arr); // [['0', 'hello'], ['1', 'world']]

Skip the Object.entries and use dataRegularSecondary directly for forEach.

As for your output, it looks like returnData is an object as well. Make sure it's an array and just push the data into that.

dataRegularSecondary.forEach(value => {
  returnData.push({
    name: value.name,
    data: [{
      y: value.y,
      total: value.total_ada,
      next: 'level_2',
      drilldown: true,
    }],
  });
});

Or you can use map as well.

const returnData = dataRegularSecondary.map(value => ({
  name: value.name,
  data: [{
    y: value.y,
    total: value.total_ada,
    next: 'level_2',
    drilldown: true,
  }],
}));

1 Comment

Thankyou for your answer, i found the best practice and check the comment from dave its work from gbu bro

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.