1 year ago

#324728

test-img

Fabrice

alasql & grouping functions: use Javascript or AlaSQL to create the resulting object?

Below the input data:

[
        {
            "name": "jim",
            "color": "blue",
            "age": "22"
        },
        {
            "name": "Sam",
            "color": "blue",
            "age": "33"
        },
        {
            "name": "eddie",
            "color": "green",
            "age": "77"
        }
]

I would like a resulting object using a single AlaSQL query:

{
        "jim": {
            "color": "blue",
            "age": "22"
        },
        "sam": {
            "color": "blue",
            "age": "33"
        },
        "eddie": {
            "color": "green",
            "age": "77"
        }
}

Is there any way to achieve it one shot with a AlaSQL query ? This would allow to easily find contact info (e.g. data["eddie"].age)

The closest way I found is this:

var res = alasql('SELECT name, ARRAY(_) AS details FROM ? GROUP BY name', [data]);

but I still obtain an array under this format:

[
    {
        "name": "jim",
        "details": [{
            "color": "blue",
            "age": "22"
        }],
    },
    {
        "name": "Sam",
        "details": [{
            "color": "blue",
            "age": "33"
        }],
    },
    {
        "name": "eddie",
        "details": [{
            "color": "green",
            "age": "77"
        }]
    }
]

What would be the fatest and efficient way to achieve the goal ?

javascript

alasql

0 Answers

Your Answer

Accepted video resources