0

I have a json as

var days = {mon: 1, tue:1, fri: 1}

What I'm trying to do it

var dayswithone =[];
['mon','tue','wed','thur','fri'].forEach(function(day){
    if(days.day){
      dayswithone.push(day);

    }
});

All i have to do is push days with 1 into array dayswithone. But this is giving me days.day as undefined.

user3803784
  • 61
  • 1
  • 4
  • 1
    possible duplicate of [JavaScript object: access variable property by name as string](http://stackoverflow.com/questions/4255472/javascript-object-access-variable-property-by-name-as-string) – Clive Jul 12 '14 at 13:01
  • sorry ! yes a duplicate question – user3803784 Jul 12 '14 at 13:04

2 Answers2

1

You should try this

var dayswithone =[];
['mon','tue','wed','thur','fri'].forEach(function(day){
if(days[day]){
  dayswithone.push(day);

}
});
Indranil Mondal
  • 2,799
  • 3
  • 25
  • 40
0

try days[day] instead of days.day;

Vlas Bashynskyi
  • 1,886
  • 2
  • 16
  • 25