Message from JavaScript discussions

November 2017

— Halp

— 

I have this object

data = {
id: 1
name: "inchidi",
status: {
id: 1,
attrs: [
{
time: "2017-10-18",
handsome: "9.5",
cool_kids: true
},
{
time: "2017-10-19",
handsome: "9.8",
cool_kids: true
}
]
}
}

and when i need the attrs array i do
var the_attrs_array = data.status.attrs;

the problem is i want to make the object format flexible, it could be data.results.attrs or data.skills.attrs etc.

— I try this:

var the_attrs = skills.attrs;
....
the_attrs_array = data.the_attrs;

but it doesnt work

— Nvm i found this SO answer https://stackoverflow.com/a/6491621/3925477

— Horrible

— Uh hater

— Use Object.keys to get an array of keys, then make a guess

— Maybe filter by object?

— Just try it, its returning the key as array but what i need is pass the key

— Actually i cant do something to the object

— Because the object crated and used in other function

— 

for (const key in data) {
const value = data[key];
if (
typeof value === 'object' &&
typeof value.attrs !== 'undefined'
) {
// value is the object that holds attrs
}
}

Message permanent page