Continuing to enhance our micro functional library, here is the mappath function.
mappath (propertypath, arrayofobjects) => arrayofpropertyvalues
Example :
let pets = [
{
petname : "Bill",
breed : "poodle",
weight: 12,
owner : {
ownername : "Paul",
contribution : {
amount : 32,
payed : false
},
address : {
city : "Paris"
}
}
},
{
petname : "Maya",
race : "pointer",
weight: 27,
owner : {
ownername : "Henri",
contribution : {
amount : 12,
payed : true
},
address : {
city : "London"
}
}
},
{
petname : "Ooper",
race : "setter",
weight: 20,
owner : {
ownername : "Nicolas",
contribution : {
amount : 12,
payed : true
},
address : {
city : "London"
}
}
}
]
Let's retrieve all the amount values;
const amountpath = "owner.contribution.amount"
console.log(mappath(amountpath, pets))
For me, that is more readable and testable, but that is a matter of taste.
You can play with demo here : DEMO
Top comments (0)