I have an array of arrays. How do I convert them to an array of objects grouped together?
var abc = [
['apple', 'fruit'], ['banana', 'fruit'], ['grapes', 'fruit'], ['red', 'color'], ['blue', 'color'], ['black', 'color']
]
abc = abc.map(function(item) {
return {
value: item[0],
color: item[1]
}
})
colors = abc.filter(function(item) {
return item.color === 'color'
})
fruits = abc.filter(function(item) {
return item.color === 'fruit'
})
var result = [{
group: 'color',
value: colors.map(function(item) {
return item.value
})
}, {
group: 'fruit',
value: fruits.map(function(item) {
return item.value
})
}]
console.log(result)
My expected output is:
var abc = [
{
group: 'color',
value: ['red', 'blue', 'black']
},
{
group: 'fruit',
value: ['apple', 'banana', 'grapes']
}]
Is there an easier way to achieve this?
I can use lodash too. Please advise.
groupvalue; by extending object IF already exists, and creating if it does not.