I have used lodash to create an array of objects from a specific key, then remove this given key from its object.
I have this
var cars = [{
"itemID": "-KUsw42xU-S1qA-y3TiI", // use this as key
"name": "Car One",
"qtd": "1"
},
{
"itemID": "-KUsw42xU-r1qA-s3TbI",
"name": "Car Two",
"qtd": "2"
}
]
Trying to get this:
var cars = {
"-KUsw42xU-S1qA-y3TiI": {
"name": "Car One",
"qtd": "1"
},
"-KUsw42xU-r1qA-s3TbI": {
"name": "Car Two",
"qtd": "1"
}
}
I have tried this approach, but I have no success.
_.chain(a)
.keyBy('itemID')
.omit(['itemID'])
.value();
You were nearly there. To omit the itemID from each object you need to map the values (using mapValues):
var result = _.chain(cars)
.keyBy('itemID')
.mapValues( v => _.omit(v, 'itemID'))
.value();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With