Reputation: 27114
Curious what I'm doing wrong here :
employee_ids = $('[data-employee_id="'+employee+'"]');
timestamp_ids = $('[data-scheduled_on="'+timestamp+'"]');
var common = $.grep(timestamp_ids, function(element) {
$.each(employee_ids, function(idx, item) {
if ( item === element ) { console.log ("omg!") };
});
});
This returns just the list of timestamp_ids
and not that array compared against employee_ids
looking for a single match.
Upvotes: 0
Views: 73
Reputation: 46647
You are not using .grep correctly. Each iteration of grep should return a boolean: true to add it to the result array, false to ignore it.
var listA = [1, 2, 3];
var listB = [2, 3, 4];
var union = $.grep(listA, function (element) {
return listB.indexOf(element) !== -1;
});
Note that IE does not support .indexOf
on Arrays, you will have to implement the comparison some other way.
EDIT: if you are trying to find a single item of an array that matches some criteria, i would suggest just using a regular for loop:
var result;
for (var i = 0; i < yourArray.length; i++) {
if (yourArray[i].id === employee_ID) { // whatever conditions you have
result = yourArray[i];
break;
}
}
if (result) {
// do whatever
} else {
// no match
}
Upvotes: 1
Reputation: 27114
Whoa! Thanks for everyone's help. I just realized I could do this :
$('[data-employee_id="'+employee+'"][data-scheduled_on="'+timestamp+'"]');
I also realized I'm an idiot :(
Upvotes: 1
Reputation: 2961
Will this work?
employee_ids = $('[data-employee_id="'+employee+'"]');
timestamp_ids = $('[data-scheduled_on="'+timestamp+'"]');
var common = $.grep(timestamp_ids, function(element) {
return !($.inArray(element, timestamp_ids) == -1)
});
Upvotes: 1
Reputation: 13105
Whatever else is wrong with that, it looks like the error is happening at $.grep
What is the typeof of timestamp_ids? According to the jQ docs, it needs to be an array.
Upvotes: 1