查找OpenLayers数组的索引

问题描述

我很难找到此特定OpenLayers数组的索引。 数组看起来像这样。

Array(7)
0: PinchRotate {disposed_: false,pendingRemovals_: {…},dispatching_: {…},listeners_: {…},revision_: 0,…}
1: PinchZoom {disposed_: false,revision_: 
0,…}
2: KeyboardPan {disposed_: false,…}
3: KeyboardZoom {disposed_: false,…}
4: MouseWheelZoom {disposed_: false,…}
5: DragBox {disposed_: false,…}
6: PointerInteraction {disposed_: false,…}

我正在尝试找到索引项=“ DragBox”的索引

我尝试了字符串和对象,即

let index =  MapValues.map.interactions.array_.indexOf("DragBox");

let obj = DragBox
let index =  MapValues.map.interactions.array_.indexOf(obj);

两个都返回-1 ...我真的很困惑如何使用打字稿中的这个特定数组来做到这一点。

非常感谢您的帮助

解决方法

要在数组中搜索特定类型的对象,可以使用

.findIndex(obj => obj instanceof DragBox)