Javascript array containing object logic failing. What is going wrong?
I have two arrays, one of the arrays holds all the unique values of another array. The second array holds the unique values of the duplicate values found of another array.
The following code below loops through the first array and checks to see i开发者_C百科f a value in that array index matches a value in the second array index for a duped item. If it does it will add an arrow before the item's text.
This doesn't seem to be working correctly and it's printing the first array, rather then checking for a duped item.
var aos = ["a","a","b","c","d","e","f","f","f","g","g","g","h","h","h"];
// Credits to the guy who helped me with this, internet w00t!
var duplicates = _.select(aos, function(val) {
var True = _.after(2, function() {
return true;
});
return _.any(aos, function(elem) {
return elem === val ? True() : false;
});
});
aos.sort();
aos = _.unique(aos);
var dennis = _.unique(duplicates);
if ($.inArray(aos[i], dennis)) {
console.log("CONTAINS!", dennis, aos[i]);
$("tr[number='" + i + "']").find("td:first-child").prepend("<img src='images/arrow.jpg' style='padding-right: 5px;'/>");
} else
console.log("no!");
Output:
no!
CONTAINS! ["a", "f", "g", "h"] b
CONTAINS! ["a", "f", "g", "h"] c
CONTAINS! ["a", "f", "g", "h"] d
CONTAINS! ["a", "f", "g", "h"] e
CONTAINS! ["a", "f", "g", "h"] f
CONTAINS! ["a", "f", "g", "h"] g
CONTAINS! ["a", "f", "g", "h"] h
What the output should be:
CONTAINS!
no!
no!
no!
no!
CONTAINS!
CONTAINS!
CONTAINS!
What could I possibly be doing wrong? If more information, or clarification is needed please let me know.
if ($.inArray(aos[i], dennis)) {
The return value from "$.inArray()" is not a boolean. It's an index. Thus to get a boolean "is it in the array?" answer, you compare to -1, which is returned when the item is not found.
if ($.inArray(aos[i], dennis) > -1) {
You could also write just ~$.inArray(aos[i], dennis)
to get a boolean, but I'm hesitant to recommend that because some grown-up is likely to yell at me.
Now, that said, there's something very important missing from the code you posted: what is "i"?
精彩评论