在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python  網絡安全  HTML/ js中,查找list中object的位置,該如何實現(xiàn)嗎?

js中,查找list中object的位置,該如何實現(xiàn)嗎?

有這么一個數(shù)組

const test = [
    {title: 'a'},
    {title:'b'},
    {title:'c'},
    {title:'d'},
    {title:'e'},
]

有沒有快捷的方式,可以快速查找其中某項的位置,
比如 someFunction(test, {title:'d'}) -> 返回 3

回答
編輯回答
維他命

https://lodash.com/里_.findIndex()就是這樣用的 返回下標

2018年8月24日 01:39
編輯回答
殘淚

function(a){

test.forEach((v,i)=>{
    if(v.title==a){
        return i
    }
})

}

2017年5月29日 18:39
編輯回答
兔寶寶

三種函數(shù)封裝 /// 自己寫了一下

function isHasElementOne(arr,value){ 
    for(var i = 0,vlen = arr.length; i < vlen; i++){ 
        if(arr[i] == value){ 
        return i; 
        } 
    } 
    return -1; 
}


clipboard.png

2017年6月28日 22:52
編輯回答
嫑吢丕

簡單對象應該可以這樣寫:

test.map(item => JSON.stringify(item)).indexOf(JSON.stringify({title: 'b'}))
// 1
2018年4月21日 21:31
編輯回答
莫小染
const test = [
    {title: 'a'},
    {title: 'b'},
    {title: 'c'},
    {title: 'd'},
    {title: 'e'}
]

function someFunction(test, obj) {
    const { title } = obj;
    for(let i = 0; i < test.length; i++){ 
        if(test[i].title == title) return i; 
    }
}

someFunction(test, {title:'d'});

// 3
2018年2月14日 09:45
編輯回答
囍槑
function someFunction(arr, option, condition) {
    //對象是否包含另一個對象
    function contain(source, target, condition) {//condition 全部包含或只包含一部分
        return Object.keys(target)[condition?"every":"some"](v=>target[v] == source[v]);//通過數(shù)組every和some方法
    }
    return arr.findIndex(v=>contain(v,option,condition));//數(shù)組的findIndex方法
}
2017年9月21日 18:52