is there an equivalent in javascript to php's in_array()?
thanks!
--Josh
is there an equivalent in javascript to php's in_array()?
thanks!
--Josh
No, I don't believe there is. I think you have to loop through to check for a value.
Actually, you could use the indexOf() function:
<script type="text/javascript">
var myTest = new Array();
myTest[] = 'foo';
myTest[] = 'bar';
if(myTest.indexOf('foobar') == -1) {
alert('No foobar found!');
}
if(myTest.indexOf('bar') != -1) {
alert('Bar was found in index #' + myTest.indexOf('bar'));
}
</script>
You have to use that, but my point was you have to loop through the array to test for anything contained in it, or absent from it.
Since indexOf() as an array method is a JavaScript 1.6 extension to the ECMA standard, you'll need to do the following so it works in IE and other browsers that do not support that extension (from this page):
<script type="text/javascript">
if (!Array.prototype.indexOf)
{
Array.prototype.indexOf = function(elt /*, from*/)
{
var len = this.length;
var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++)
{
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};
}
var myTest = new Array();
myTest.push('foo');
myTest.push('bar');
if(myTest.indexOf('foobar') == -1) {
alert('No foobar found!');
}
if(myTest.indexOf('bar') != -1) {
alert('Bar was found in index #' + myTest.indexOf('bar'));
}
</script>
maybe it would help if i explained what im trying to do. i have this function which is a callback for a js date picker. it returns true if the date should be disabled, and false if the date should not be disabled. currently blocks past dates, and dates a certain distance in the future defined by MAX_ADV_TIME.
function dateStatus(date) {
var min = new Date('<?php echo date('m/d/Y'); ?>');
var max = new Date('<?php echo date('m/d/Y', time() + MAX_ADV_TIME); ?>');
if(date.getTime() < min.getTime() || date.getTime() > max.getTime()){
return true; // true says "disable"
}else{
return false; // leave other dates enabled
}
};
i want to be able to pass an array of dates that should be disabled in the date picker. i want to pass the array from php to js (should i use json? and if so how?).
any help would be great! thanks!!
Well I would do it completely differently. I would grab the current date and have a fixed number of additions to it.
i found another work around in a forum thread on the website for the date selector. thank you for your help.