2016-10-26 162 views
2

如果我有一個包含對象,每個對象都有一個id屬性的數組,我可以用找到索引:在二維數組查找指數

data.findIndex(x=>x.id === newData.id); 

但是,如果數據是對象數組的數組?有沒有一種很好的方法來獲得這個數據結構的兩個索引?所以data.findIndex將以某種方便的形式返回ij

+0

莫不是每個嵌套陣列中的多個對象? – RomanPerekhrest

+0

http://stackoverflow.com/a/16102897/1398867而不是原語,檢查對象上的'id' theArray [i] .id == valueToSearch' – Venugopal

回答

1

這是我對基於Array.prototype.reduce()一個遞歸查找索引,可以處理多層嵌套:

const recursiveFindIndex = (data, id) => 
 
    data.reduce((indexes, item, index) => { 
 
    let subIndex; 
 

 
    Array.isArray(item) && (subIndex = recursiveFindIndex(item, id)); 
 

 
    if (subIndex && subIndex.length) { 
 
     return indexes.concat([index], subIndex); 
 
    } 
 

 
    item.id === id && (indexes.push(index)); 
 

 
    return indexes; 
 
    }, []); 
 

 
const data = [ 
 
    { id: 0 }, 
 
    [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }], 
 
    [{ id: 6 }, { id: 7 }, { id: 8 }, { id: 9 }, [ 
 
    { id: 10 }, { id: 11 }, { id: 12 }, { id: 13 }, { id: 14 }] 
 
    ] 
 
]; 
 

 
console.log('id: 3', recursiveFindIndex(data, 0)); 
 
console.log('id: 3', recursiveFindIndex(data, 3)); 
 
console.log('id: 6', recursiveFindIndex(data, 8)); 
 
console.log('id: 12', recursiveFindIndex(data, 12)); 
 
console.log('id: 3', recursiveFindIndex(data, 20));

以及更通用的高性能(它將停止搜索和只要找到匹配就返回)使用for循環的版本:

const recursiveFindIndex = (arr, predicate) => { 
 
    let subIndex; 
 
    
 
    for(let i = 0; i < arr.length; i++) { 
 
    if(Array.isArray(arr[i])) { 
 
     subIndex = recursiveFindIndex(arr[i], predicate); 
 
     
 
     if(subIndex !== -1) { 
 
     return [i].concat(subIndex); 
 
     } 
 
    } else if(predicate(arr[i])) { 
 
     return [i]; 
 
    } 
 
    } 
 

 
    return -1; 
 
}; 
 

 
const data = [ 
 
    { id: 0 }, 
 
    [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }], 
 
    [{ id: 6 }, { id: 7 }, { id: 8 }, { id: 9 }, [ 
 
    { id: 10 }, { id: 11 }, { id: 12 }, { id: 13 }, { id: 14 }] 
 
    ] 
 
]; 
 

 
console.log('id: 0', recursiveFindIndex(data, (item) => item.id === 0)); 
 
console.log('id: 3', recursiveFindIndex(data, (item) => item.id === 3)); 
 
console.log('id: 6', recursiveFindIndex(data, (item) => item.id === 8)); 
 
console.log('id: 12', recursiveFindIndex(data, (item) => item.id === 12)); 
 
console.log('id: 20', recursiveFindIndex(data, (item) => item.id === 20));

0

那麼,這樣的事情?

它可能不是最優雅的解決方案,但它的工作原理:

const newData = { id: 1 }; 
const data = [[{id: 0}, {id: 1}], [{id: 2}, {id: 3}]]; 

data.reduce((res, x, i) => { 
    let j = x.findIndex(y => y.id === newData.id); 

    if (~j) { 
    return [i, j]; 
    } 

    return res; 
}, [-1, -1]); 
0

您可以使用Array.includesfindIndex如果值的數組。

var data = [ 
 
    [1,2,3], 
 
    [4,5,6], 
 
    [7,8,9] 
 
] 
 
var searchParam = 8; 
 
var index = data.findIndex(x=>x.includes(searchParam)) 
 
console.log(index)

如果它的陣列的進一步級別對象的數組,你可以使用遞歸。

var data = [ 
 
    [{id: 1},{id: 2},{id: 3}], 
 
    [{id: 4},{id: 5},{id: 6}], 
 
    [{id: 7},{id: 8},{id: 9}] 
 
] 
 
var searchValue = 8; 
 
var index = data.findIndex(x=>{ 
 
    return searchInObject(x, searchValue); 
 
}) 
 

 
function searchInObject(obj, searchValue){ 
 
    var _s = JSON.stringify(obj); 
 
    if(_s.indexOf(searchValue)>-1){ 
 
    if(Array.isArray(obj)){ 
 
     return obj.some(function(o){ 
 
     if(searchInObject(o, searchValue)) return true; 
 
     }); 
 
    } 
 
    else if(typeof(obj) === 'object'){ 
 
     for(var k in obj){ 
 
     if(searchInObject(obj[k], searchValue)) return true; 
 
     } 
 
    } 
 
    else{ 
 
     if(obj === searchValue) return true; 
 
    } 
 
    } 
 
} 
 
console.log(index)

0

另一個使用reduce

const data = [ 
    [{ id : 1}, { id: 4 }], 
    [{ id :2 }, { id : 3}] 
] 

const findIJ = (data, id) => 
    data.reduce((p, c, i) => { 
    let j = c.findIndex(x => x.id === id); 
    if(j > - 1){ 
     p = { i, j } 
    } 
    return p; 
    }, { i : -1, j : -1 }); 

https://jsfiddle.net/05cubh2b/