Answers for "lodash Group By"

0

lodash Group By

_.groupBy( collection, iteratee )
Posted by: Guest on June-08-2021
0

lodash groupby return array

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);
Posted by: Guest on May-25-2021
0

lodash groupby return array

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
}
Posted by: Guest on May-25-2021
0

lodash groupby return array

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
Posted by: Guest on May-25-2021
-1

lodash map

const _markets_arr = []

_.map(markets, (val, key) => {
   val.symbol = key
   if (val.buys > 0) {
      _markets_arr.push(val)
   }
})

console.log(_markets_arr)
//=> [ 
       { buys: 3, sells: 1, symbol: 'DASH/BTC' },
       { buys: 3, sells: 2, symbol: 'ETH/BTC' } 
   ]
Posted by: Guest on May-12-2020

Browse Popular Code Answers by Language