3 ms·
I'm not sure what you're looking for, but a fun problem I ran into one day (one that I solved, but ultimately we went a different route) is to find an existing
by adamredwoods 5y ago
I'm not sure what you're looking for, but a fun problem I ran into one day (one that I solved, but ultimately we went a different route) is to find an existing key in a js object, and insert a new key/value js object after that key.
For example:
const userdata = [
{
name: "Emma",
age: 21,
hobby: {
reading: true,
gaming: false,
sport: "tennis"
},
class: ["Java", "Go", "Nginx"]
},
{
name: "John",
age: 22,
hobby: {
reading: true,
gaming: false,
sport: "football"
},
class: ["JavaScript", "HTML", "CSS"]
},
]
const newdata = {
knitting: true
}
And then, the task is to make a utility function to find "name: John", then "hobby" and append newdata in the "hobby"object. Try to apply immutability. Try to do without the overhead of JSON string conversion.
- LecroJS 5y agoMaybe I’m misunderstanding the problem but any reason why you can’t just call .map on the input array and if el.name === John -> el.hobby[knitting] = true, then return el regardless?
- adamredwoods 5y agoYou could if the object was a known structure, but I needed a function that could be a utility for any object (some were quite large), something like: const newobj = insertIntoObject( obj, ["name:John", "hobby:*"], newdata )
- bobblywobbles 5y agoInteresting, yes I think that would make a good post - what's the best way to get a hold of you? Do you prefer email?
- adamredwoods 5y agosure my email is on my site.