Working on some code and I’m running into an issue I don’t know how to fix using javascript.
I have days that is a collection of objects that look like:
day: 1
a: 10
b: 5
c: 9
d: 6
and so forth.
What I want to do is have a function that I can pass days and one of the id’s for a, b, c, d and have it do ‘something’. (By something, I’m not sure yet).
I’m just struggling to figure out how to pass the identifier.
function returnAll(data, id){
for (var day in data){
console.log('On day' + day + ' , ' + id + ' has a value of ' data[day].id)
}
}
Which would ideally put something like this into the console:
On day 1 , a has a value of …
On day 2, a has a value of …
When returnAll(days, a) is called, I just can’t get the id to link up properly inside of the function. I’ll need to call this function for either a, b, c…The days can be dynamic so I need a function that operates just on the values of the id passed as the parameter.
Any thoughts?
Try this to access the desired property inside your function:
Call it like this: