“Lodash Groupby” Réponses codées

Diff deux tableaux Lodash

_.difference([2, 1], [2, 3]);
// => [1]
Bright Beetle

Tableau de retour de Lodash Groupby

export const groupArrayBy = (arr, groupBy) => {

    let newArr = []
    arr.map((item) => {
        if(item[groupBy]) {
            let finded = newArr.filter((newItem) => newItem[groupBy] === item[groupBy])
            if(finded.length > 0) {
                finded[0].products.push(item)
            } else {
                newArr.push({category: item[groupBy], products: [item]})
            }
        }
    })

    return newArr
}

Zany Zebra

Tableau de retour de Lodash Groupby

   let arr = [{
	"birthdate": "1993",
	"name": "Ben"
},
{
	"birthdate": "1994",
	"name": "John"
},
{
	"birthdate": "1995",
	"name": "Larry"
},
{
	"birthdate": "1995",
	"name": "Nicole"
},
{
	"birthdate": "1996",
	"name": "Jane"
},
{
	"birthdate": "1996",
	"name": "Janet"
},
{
	"birthdate": "1996",
	"name": "Dora"
},
];

const res = arr.reduce((ac, a) => {
let temp = ac.find(x => x.birthdate === a.birthdate);
if (!temp) ac.push({ ...a,
	name: [a.name]
})
else temp.name.push(a.name)
return ac;
}, [])
console.log(res);
Zany Zebra

Lodash Groupby

Creates an object composed of keys generated from the results of running 
each element of collection thru iteratee. The order of grouped values is 
determined by the order they occur in collection. The corresponding value 
of each key is an array of elements responsible for generating the key. 
The iteratee is invoked with one argument: (value).

_.groupBy([6.1, 4.2, 6.3], Math.floor);
// => { '4': [4.2], '6': [6.1, 6.3] }
 
// The `_.property` iteratee shorthand.
_.groupBy(['one', 'two', 'three'], 'length');
// => { '3': ['one', 'two'], '5': ['three'] }
deadman

Format de date de chargement

_.defer(function(stamp) {  console.log(_.now() - stamp);}, _.now());// => Logs the number of milliseconds it took for the deferred invocation.
Poised Piranha

Réponses similaires à “Lodash Groupby”

Questions similaires à “Lodash Groupby”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code