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,
"name_alt": ["Kiurinsty", "Kiurinty", "Lezghi", "Lezgi", "Lezgian", "Lezgin"],
"country_main": "ru"
}]
I have written this function which takes the array, maps and filters it to return the data set I require. It works, but I am concerned it's not very performant. It takes the array that I want to compress (array
), the property that I want to group by (mappedProperty
), the property that should remain unique for every row (uniqueProperty
) and a property that should be excluded from the set of grouped properties (mainProperty
)
Could you review?
function reduceRows(array, mappedProperty, uniqueProperty, mainProperty) {
var currentUniqueProp;
var objToReturn;
var mappedObj = _.map(array, function(obj) {
// we're grouping by the unique property (i.e. an ID), so if we have a new
// unique property, we clone the object, and start adding the
// mapped property (i.e. alternative names for a language) to the array
//usage: u.reduceRows(data.data.languages, "name_alt", "language_id", "language_name")
if (obj[uniqueProperty] != currentUniqueProp) {
objToReturn = _.clone(obj)
objToReturn[mappedProperty] = [obj[mappedProperty]]
currentUniqueProp = obj[uniqueProperty]
} else {
// 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
if (obj[mappedProperty] != obj[mainProperty]) {
objToReturn[mappedProperty].push(obj[mappedProperty])
}
}
return objToReturn;
})
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
} else {
currentUniqueProp = obj[uniqueProperty]
return true
}
})
return filteredObj
}
else if
but if yes you should use it at the firstif..else
. \$\endgroup\$else if
\$\endgroup\$objToReturn
is a confusing name. It should describe what it represents and not what you plan on doing with it later. \$\endgroup\$