Created
March 11, 2022 14:44
-
-
Save mauroquinteros/7997d5a40707696f0da8bdd5d49a188a to your computer and use it in GitHub Desktop.
Group objects by key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { data } from "./data.js"; | |
const serializeChannel = (channel) => ({ | |
id: channel.SK, | |
channel: channel.nameChannel, | |
}); | |
function getUniqueArrayByKey(dataList, key = "SK") { | |
const arrayMap = dataList.map((data) => [data[key], serializeChannel(data)]); | |
const uniqueMap = new Map(arrayMap); | |
return [...uniqueMap.values()]; | |
} | |
const uniqueArray = getUniqueArrayByKey(data); | |
console.table(uniqueArray); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const data = [ | |
{ | |
infoText: "SMS con número remitente de 9 dígitos", | |
GSI1PK: "SENDTYPE", | |
SK: "CHANNEL#1", | |
name: "SMS LARGO", | |
PK: "SENDTYPE#1", | |
shortName: "LARGO", | |
nameChannel: "SMS", | |
}, | |
{ | |
infoText: "SMS con número remitente de 5 dígitos", | |
GSI1PK: "SENDTYPE", | |
SK: "CHANNEL#1", | |
name: "SMS CORTO SIMPLE", | |
PK: "SENDTYPE#2", | |
shortName: "CORTO SIMPLE", | |
nameChannel: "SMS", | |
}, | |
{ | |
infoText: "SMS tipo pop up", | |
GSI1PK: "SENDTYPE", | |
SK: "CHANNEL#1", | |
name: "SMS CORTO FLASH", | |
PK: "SENDTYPE#3", | |
shortName: "CORTO FLASH", | |
nameChannel: "SMS", | |
}, | |
{ | |
infoText: | |
"Si no se tiene éxito en la llamada se deja el audio en el buzón de voz", | |
GSI1PK: "SENDTYPE", | |
SK: "CHANNEL#2", | |
name: "IVR NORMAL", | |
PK: "SENDTYPE#4", | |
shortName: "NORMAL", | |
nameChannel: "IVR", | |
}, | |
{ | |
infoText: | |
"Si no se tiene éxito en la llamada se deja el audio en el buzón de voz, además brinda los estados adicionales Human...", | |
GSI1PK: "SENDTYPE", | |
SK: "CHANNEL#2", | |
name: "IVR AMD", | |
PK: "SENDTYPE#5", | |
shortName: "AMD", | |
nameChannel: "IVR", | |
}, | |
{ | |
infoText: | |
"Se corta la llamada cuando detecta el buzón de voz, además brinda los estados adicionales Human (Detectó una voz humana).", | |
GSI1PK: "SENDTYPE", | |
SK: "CHANNEL#2", | |
name: "IVR AMD CORTE", | |
PK: "SENDTYPE#6", | |
shortName: "AMD CORTE", | |
nameChannel: "IVR", | |
}, | |
{ | |
infoText: "", | |
GSI1PK: "SENDTYPE", | |
SK: "CHANNEL#3", | |
name: "EMAIL", | |
PK: "SENDTYPE#7", | |
shortName: "EMAIL", | |
nameChannel: "EMAIL", | |
}, | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cars = [ | |
{ make: "audi", model: "r8", year: "2012" }, | |
{ make: "audi", model: "rs5", year: "2013" }, | |
{ make: "ford", model: "mustang", year: "2012" }, | |
{ make: "ford", model: "fusion", year: "2015" }, | |
{ make: "kia", model: "optima", year: "2012" }, | |
], | |
result = cars.reduce(function (r, a) { | |
r[a.make] = r[a.make] || []; | |
r[a.make].push(a); | |
return r; | |
}, Object.create(null)); | |
console.log(result); | |
function groupByKey(array, key) { | |
return array.reduce((hash, obj) => { | |
if (obj[key] === undefined) return hash; | |
return Object.assign(hash, { | |
[obj[key]]: (hash[obj[key]] || []).concat(obj), | |
}); | |
}, {}); | |
} | |
var cars = [ | |
{ make: "audi", model: "r8", year: "2012" }, | |
{ make: "audi", model: "rs5", year: "2013" }, | |
{ make: "ford", model: "mustang", year: "2012" }, | |
{ make: "ford", model: "fusion", year: "2015" }, | |
{ make: "kia", model: "optima", year: "2012" }, | |
]; | |
console.log(groupByKey(cars, "make")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment