-2

I have been reading some posts which are related to my question, whereas I have not been able to find a proper solution for what I'm trying to do hear.

I have this JSON file that I am obtaining directly from my database via a sql query:

[
    {
        "scenario": "scenario-483d742c-4492-4a4f-95fa-7ccceac8bb18",
        "data": [
            {
                "date": "2018-05-21",
                "price": 14.173041264216105
            }
        ]
    },
    {
        "scenario": "scenario-483d742c-4492-4a4f-95fa-7ccceac8bb18",
        "data": [
            {
                "date": "2018-05-22",
                "price": 42.94691197077433
            }
        ]
    }, 
    {
        "scenario": "scenario-c705069f-fa53-4ff3-9f07-3fcbf9dc8d15",
        "data": [
            {
                "date": "2018-05-22",
                "price": 42.94691197077433
            }
        ]
    },
    {
        "scenario": "scenario-c705069f-fa53-4ff3-9f07-3fcbf9dc8d15",
        "data": [
            {
                "date": "2018-05-22",
                "price": 42.94691197077433
            }
        ]
    },
    {
        "scenario": "scenario-d58bb001-d7ed-4744-8f6c-8377519c7a99",
        "data": [
            {
                "date": "2018-05-22",
                "price": 42.94691197077433
            }
        ]
    },
    {
        "scenario": "scenario-d58bb001-d7ed-4744-8f6c-8377519c7a99",
        "data": [
            {
                "date": "2018-05-22",
                "price": 42.94691197077433
            }
        ]
    }
]

My objectif is to be able to sort/transform this json object to a new json object which is classified by the scenario, so, something that looks like:

[
    {
        "scenario": "scenario-483d742c-4492-4a4f-95fa-7ccceac8bb18",
        "data": [
            {
                "date": "2018-05-21",
                "price": 14.173041264216105
            },
            {
                "date": "2018-05-22",
                "price": 42.94691197077433
            }
        ]
    },
    {
        "scenario": "scenario-c705069f-fa53-4ff3-9f07-3fcbf9dc8d15",
        "data": [
            {
                "date": "2018-05-22",
                "price": 42.94691197077433
            },
            {
                "date": "2018-05-22",
                "price": 42.94691197077433
            }

        ]
    },
    {
        "scenario": "scenario-d58bb001-d7ed-4744-8f6c-8377519c7a99",
        "data": [
            {
                "date": "2018-05-22",
                "price": 42.94691197077433
            },  
            {
                "date": "2018-05-22",
                "price": 42.94691197077433
            }
 ]

I have been trying some javascript selfmade functions but I have not obtainend the desired result. This is the last thing I've tried:

 let estructura = [];

    for (var j =0; j<obj.length; j++){
      for (var i=0; i<estructura.length; i++){
        if(obj[j]['scenario'] == estructura[i]['scenario']){
          estructura[i]['data'].push(obj[j]['data'])
        } else {
          console.log("no match, we add the scenario to estructura")
          estructura.push(
            {
              scenario:obj[j]['scenario'],
              data: []
            })
        }
      }

    }

Thank you

4

1 Answer 1

9

You can achieve this easily with built-in functions like reduce. Iterate over the input, grouping into an object indexed by scenario, creating a object with a data array if the scenario doesn't exist yet, and push to that array:

const input=[{"scenario":"scenario-483d742c-4492-4a4f-95fa-7ccceac8bb18","data":[{"date":"2018-05-21","price":14.173041264216105}]},{"scenario":"scenario-483d742c-4492-4a4f-95fa-7ccceac8bb18","data":[{"date":"2018-05-22","price":42.94691197077433}]},{"scenario":"scenario-c705069f-fa53-4ff3-9f07-3fcbf9dc8d15","data":[{"date":"2018-05-22","price":42.94691197077433}]},{"scenario":"scenario-c705069f-fa53-4ff3-9f07-3fcbf9dc8d15","data":[{"date":"2018-05-22","price":42.94691197077433}]},{"scenario":"scenario-d58bb001-d7ed-4744-8f6c-8377519c7a99","data":[{"date":"2018-05-22","price":42.94691197077433}]},{"scenario":"scenario-d58bb001-d7ed-4744-8f6c-8377519c7a99","data":[{"date":"2018-05-22","price":42.94691197077433}]}]

console.log(
  Object.values(input.reduce((a, { scenario, data }) => {
    if (!a[scenario]) a[scenario] = { scenario, data: [] };
    a[scenario].data.push(data[0]);
    return a;
  }, {}))
);

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

1 Comment

Thank you very much

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.