user3214546
user3214546

Reputation: 6831

How can i convert days of week selected into , separated integers in javascript

I have the checkboxes of week days and user select them

The JS object is like this

{"mon":true,"tue":true,"wed":true,"thu":false,"fri":false,"sat":false,"sun":false}

Now i want to convert it into string of , separated value like.

0,1,2 to store in database where monday is 0 and so on. so if only sunday is selected then it will be

7 only

I have tried this

 var b = days;
 days = days.map(function (a) {
  return b.indexOf(a);
            });

Upvotes: 0

Views: 639

Answers (4)

jfriend00
jfriend00

Reputation: 707546

You can do this which uses a lookup table to convert days of the week to day numbers, accumulates the selected numbers in an array and then turns the array into a comma separated string.

function getDayList(data) {
    var dayNumMap = {"mon":1,"tue":2,"wed":3,"thu":4,"fri":5,"sat":6,"sun":7};
    var list = [], item;
    // iterate all days pass in
    for (var day in data) {
        // if the checkbox was selected
        if (data[day]) {
            item = dayNumMap[day];
            // if it's valid data
            if (item) {
                // add the day number to the list
                list.push(item);
            }
        }
    }
    // return a comma separated string
    return list.sort().join(",");
}

// data from checkboxes
var formData = {"mon":true,"tue":true,"wed":true,"thu":false,"fri":false,"sat":false,"sun":false};

var str = getDayList(formData);

Upvotes: 1

Amitesh
Amitesh

Reputation: 1517

The problem is you are implicitely assuming that your object property order will remain same while iterating and thus you can easily map your property name to index. e.g "mon" mapped to 0. But this is not a guaranteed cross browser scenario.

The for..in statement iterates over the enumerable properties of an object, in arbitrary order.

Therefore to have a more robust and predictable approach you should add the mapping value to object itself

var days = {"mon":{index:0,value:true},"tue":{index:1,value:true}};
var val = getConcatanatedDays(days);
function getConcatanatedDays(obj) {
    var val = [];
    for(var prop in obj) {
        if(obj.hasOwnProperty(prop) && obj[prop].value) {
            val.push(obj[prop].index);
        }
    }
    return val.join(',');
}

You may also consider to store the days in array so that you dont have to store the day val separately and you will get the flexibility of looping through array.

var days = [{"mon":true},{}];//ordered iteration

Upvotes: 0

Rick Hitchcock
Rick Hitchcock

Reputation: 35670

A different approach, which should work if some of the object's properties are missing or out of order:

var obj = {"mon":true,"tue":true,"wed":true,"thu":false,"fri":false,"sat":false,"sun":false}

var s= '';
for(var i in obj) {
  if(obj[i]) {
    s+= 'montuewedthufrisatsun'.indexOf(i)/3;
  }
}
s= s.split('').sort().join(',')
alert(s);

Upvotes: 0

user2575725
user2575725

Reputation:

var json = {
  "mon": true,
  "tue": true,
  "wed": true,
  "thu": false,
  "fri": false,
  "sat": true,
  "sun": false
};

var sel = [], i = 0;
$.each(json, function(a, b) {
  if (b)
    sel.push(i);//<-- selected index
  i++;
});

$('#out').text('selected: ' + sel.join(','));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="out">please wait ...</div>

Upvotes: 0

Related Questions