1

Is there a way to get indexOf on this array?

var myArray = new Array(
 {title: "Test1", id: "Header1", html: "Header1", style: "background-color: red; color: white;"});
James
22.4k5 gold badges30 silver badges44 bronze badges
asked Oct 26, 2011 at 20:53
4
  • 3
    What do you mean by "indexOf"? Can you explain the operation you'd like to perform, in other words? There's only one element in the array, so it's index is guaranteed to be zero. Commented Oct 26, 2011 at 20:57
  • the one element thing was done just for brevity -- I want to check the index of the id of 'Header1' in myArray. Since this is kind of an associative array, I wasnt sure how to use the indexOf method that comes with array object. Looks like I can't. Commented Oct 26, 2011 at 21:15
  • Well, it's not an associative array - it's just an array; that's the thing. I don't know of any "indexOf" implementation that allows for a "comparator" function parameter, though that'd be possible. Commented Oct 26, 2011 at 21:16
  • i dont understand, the array is containing objects, or this would be an associative array? difference: array cont. objs.: new Array({a:'a',b:'b',c:'c'},{a:'d',b:'e',c:'f'}) associative array is not array, this is an obj: associative = {a:'a',b:'b',c:'c'} Commented Oct 26, 2011 at 21:28

2 Answers 2

2

If you want to be able to do a search for that object, if you have the same reference, sure.

var o = {"foo": "bar"};
var a = [o];
alert(a.indexOf(o));

But this won't work:

alert(a.indexOf({"foo": "bar"}));

http://jsfiddle.net/qtjWm/

answered Oct 26, 2011 at 20:58
Sign up to request clarification or add additional context in comments.

1 Comment

ehh. i wouldn't figured this out by myself!
0

This is what I did to make it work -- but it's not pretty:

//Check to see if we are going to a card that should be right or left.
var rightOrLeft = "left";
var destinationIndex = 0;
for(var i=0; i < dynamicPreezeArray.length; i++){
 if (dynamicPreezeArray[i].id == this.text) {
 destinationIndex = i;
 };
}
if (destinationIndex < currentCarouselIndex) {
 rightOrLeft = "right";
}
preezer.setActiveItem(this.text,{type:'slide',direction: rightOrLeft});
currentCarouselIndex = preezer.getActiveIndex(); 

This was all just to make my Sencha carousel look right when clicking buttons, but now I realize that if someone slides it manually that it wont set the currentCarouselIndex... bleh, this is a lot of work just to make it look a little better.

answered Oct 26, 2011 at 21:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.