HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Function that groups rows returned from SQL DB

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
rowsreturnedgroupssqlfunctionthatfrom

Problem

I have a data set returned from an SQL database contained below, as you can see all data remains the same apart from one property, "name_alt".

[{
    "language_id": "tly",
    "language_name": "Talysh",
    "speakers_native": 800000,
    "total_speakers": 912000,
    "name_alt": "Taleshi",
    "country_main": "az"
}, {
    "language_id": "tly",
    "language_name": "Talysh",
    "speakers_native": 800000,
    "total_speakers": 912000,
    "name_alt": "Talish",
    "country_main": "az"
}, {
    "language_id": "tly",
    "language_name": "Talysh",
    "speakers_native": 800000,
    "total_speakers": 912000,
    "name_alt": "Talishi",
    "country_main": "az"
}, {
    "language_id": "tly",
    "language_name": "Talysh",
    "speakers_native": 800000,
    "total_speakers": 912000,
    "name_alt": "Talysh",
    "country_main": "az"
}, {
    "language_id": "tly",
    "language_name": "Talysh",
    "speakers_native": 800000,
    "total_speakers": 912000,
    "name_alt": "Talyshi",
    "country_main": "az"
}, {
    "language_id": "lez",
    "language_name": "Lezghian",
    "speakers_native": 171400,
    "total_speakers": 428400,
    "name_alt": "Kiurinsty",
    "country_main": "ru"
}, {
    "language_id": "lez",
    "language_name": "Lezghian",
    "speakers_native": 171400,
    "total_speakers": 428400,
    "name_alt": "Kiurinty",
    "country_main": "ru"
}, {
    "language_id": "lez",
    "language_name": "Lezghian",
    "speakers_native": 171400,
    "total_speakers": 428400,
    "name_alt": "Lezghi",
    "country_main": "ru"
}]


I want to combine all the name_alts into an array, so my data looks like the below:

```
[{
"language_id": "tly",
"language_name": "Talysh",
"speakers_native": 800000,
"total_speakers": 912000,
"name_alt": ["Talesh", "Taleshi", "Talish", "Talishi", "Talyshi"],
"country_main": "az"
}, {
"language_id": "lez",
"language_name": "Lezghian",
"speakers_native": 171400,
"total_speakers": 428400,
"nam

Solution

As javascript has a else if statement you should use this to remove one level of indention.

if (obj[uniqueProperty] != currentUniqueProp) {
    objToReturn = _.clone(obj)
    objToReturn[mappedProperty] = [obj[mappedProperty]]
    currentUniqueProp = obj[uniqueProperty]
} else if (obj[mappedProperty] != obj[mainProperty]) {
    // sometimes the grouped property has a value which is the same as the main property
    // i.e. the language_name is also included in the list of name_alts
    // We want to avoid this duplication

    objToReturn[mappedProperty].push(obj[mappedProperty])
}


The underscore filter method can be simplified to

var filteredObj = _.filter(mappedObj, function(obj) {
    //this function ensures that only one entry for each unique property
    //is returned
    if (obj[uniqueProperty] == currentUniqueProp) {
        return false
    }
    currentUniqueProp = obj[uniqueProperty]
    return true
})


If the condition evaluates to true the method returns, so no else is needed.

You should always declare your variables where they are needed. So var objToReturn should be declared here

var mappedObj = _.map(array, function(obj) {
        var objToReturn;
        // we're grouping by the unique property

Code Snippets

if (obj[uniqueProperty] != currentUniqueProp) {
    objToReturn = _.clone(obj)
    objToReturn[mappedProperty] = [obj[mappedProperty]]
    currentUniqueProp = obj[uniqueProperty]
} else if (obj[mappedProperty] != obj[mainProperty]) {
    // sometimes the grouped property has a value which is the same as the main property
    // i.e. the language_name is also included in the list of name_alts
    // We want to avoid this duplication

    objToReturn[mappedProperty].push(obj[mappedProperty])
}
var filteredObj = _.filter(mappedObj, function(obj) {
    //this function ensures that only one entry for each unique property
    //is returned
    if (obj[uniqueProperty] == currentUniqueProp) {
        return false
    }
    currentUniqueProp = obj[uniqueProperty]
    return true
})
var mappedObj = _.map(array, function(obj) {
        var objToReturn;
        // we're grouping by the unique property

Context

StackExchange Code Review Q#68964, answer score: 2

Revisions (0)

No revisions yet.