-4

I have an object such that, I want to make the Months false, on/before current month.

Before:

var oObj = {
                "isJan" : true,
                "isFeb" : true,
                "isMar" : true,
                "isApr" : true,
                "isMay" : true,
                "isJun" : true,
                "isJul" : true,
                "isAug" : true,
                "isSep" : true,
                "isOct" : true,
                "isNov" : true,
                "isDec" : true
            }

Today is 5-Apr, hence object should be:

            {
                "isJan" : false,
                "isFeb" : false,
                "isMar" : false,
                "isApr" : false,
                "isMay" : true,
                "isJun" : true,
                "isJul" : true,
                "isAug" : true,
                "isSep" : true,
                "isOct" : true,
                "isNov" : true,
                "isDec" : true
            }
adirocks27
  • 141
  • 1
  • 6
  • 15

6 Answers6

1

try this

var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
/*var inputObj = {
  "isJan": true,
  "isFeb": true,
  "isMar": true,
  "isApr": true,
  "isMay": true,
  "isJun": true,
  "isJul": true,
  "isAug": true,
  "isSep": true,
  "isOct": true,
  "isNov": true,
  "isDec": true
};*/
var currentMonth = new Date().getMonth();
var outputObj = {};
months.forEach( function(key, index){
  outputObj[ "is" + key ] = ( index <= currentMonth );
});
console.log( outputObj );
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

You can use the for-in loop as shown by others. However, you also want to make sure that the key you get is an actual property of an object, and doesn't come from the prototype:

for (var key in p) {
if (p.hasOwnProperty(key)) {
console.log(key + " -> " + p[key]);
}
}

Hope this will help!

0

You can do something like this:

var keys = Object.keys(oObj)

which gives all the keys in array format. Then you can use simple for loop to parse through it.

for(var i=0; i<keys.length; i++){
   var individual_obj = oObj[key[i]] ;
   // set your conditions here
}
Muhammed Neswine
  • 2,028
  • 1
  • 20
  • 20
0

The solution using Object.keys(), Array.prototype.indexOf() and String.prototype.slice() functions:

var oObj = {
        "isJan" : true,"isFeb" : true,"isMar" : true,"isApr" : true,"isMay" : true,"isJun" : true,"isJul" : true,"isAug" : true,"isSep" : true,"isOct" : true,"isNov" : true,"isDec" : true
    },
    monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ],
    currentM = new Date().getMonth();

Object.keys(oObj).forEach(function (k) {
    oObj[k] = monthNames.indexOf(k.slice(2)) > currentM;
});

console.log(oObj);
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • I guess it should be `slice(0, 3)`. By the way, this is a good answer. EDIT: Woops. My fail. Thought that the list were month names (`Januany`, etc). The slice is fine. – Jorge Fuentes González Jan 12 '18 at 00:02
-1
  • Take the keys
  • Get the current month and add 1 (since you want to avoid april too)
  • loop till the month value and make it false

That's it.

var oObj = {
  "isJan": true,
  "isFeb": true,
  "isMar": true,
  "isApr": true,
  "isMay": true,
  "isJun": true,
  "isJul": true,
  "isAug": true,
  "isSep": true,
  "isOct": true,
  "isNov": true,
  "isDec": true
}
var k = Object.keys(oObj);
var month = new Date().getMonth() + 1;
console.log(i);
for (var i = 0; i < month; i++) {
  oObj[k[i]] = false;
}

console.log(oObj);
Sagar V
  • 12,158
  • 7
  • 41
  • 68
  • JavaScript do not ensure that keys are listed in the order they are added to the object. Altough that is how it works in V8 now, they can change it in favour of optimization without breaking the standard. Maps are the way to go if you want to list keys in the same order they were added. – Jorge Fuentes González Jan 12 '18 at 00:00
-1

You need to do two things

  1. Loop over the object you can use Object.keys to get array of object's keys

  2. Make a condition inside the loop To check if the month in the object's keys before the current month or not. for this you may use getMonthfunction from Date object

Example:

const oObj = {
 "isJan" : true,
    "isFeb" : true,
    "isMar" : true,
    "isApr" : true,
    "isMay" : true,
    "isJun" : true,
    "isJul" : true,
    "isAug" : true,
    "isSep" : true,
    "isOct" : true,
    "isNov" : true,
    "isDec" : true
}
const now = new Date();
const currentMonth = now.getMonth();
const keys = Object.keys(oObj);

for (var i = 0; i < keys.length; i += 1){
      oObj[keys[i]] = currentMonth < i;
}
console.log(oObj);
Mohamed Abbas
  • 2,228
  • 1
  • 11
  • 19
  • JavaScript do not ensure that keys are listed in the order they are added to the object. Altough that is how it works in V8 now, they can change it in favour of optimization without breaking the standard. Maps are the way to go if you want to list keys in the same order they were added. – Jorge Fuentes González Jan 12 '18 at 00:00