获取indexOf Javascript数组由Javascript对象组成

时间:2011-10-26 20:53:56

标签: javascript arrays

有没有办法在这个数组上获得indexOf?

var myArray = new Array(
    {title: "Test1", id: "Header1", html: "Header1", style: "background-color: red; color: white;"});

2 个答案:

答案 0 :(得分:2)

如果您希望能够搜索该对象,如果您有相同的参考,请确定。

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

但这不起作用:

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

http://jsfiddle.net/qtjWm/

答案 1 :(得分:0)

这就是我所做的让它发挥作用 - 但它并不漂亮:

//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();   

这一切只是为了让我的Sencha旋转木马在点击按钮时看起来正确,但现在我意识到,如果有人手动滑动它不会设置当前CarouselIndex ... bleh,这是很多工作只是为了使它看起来好一点。