2016-11-12 312 views
2

我有一個對象數組,我想刪除其中一個對象。Javascript:在數組中查找對象的索引,不知道索引,只有對象

[ 
    {"field":"ingredients","gte":"egg","lte":"egg"}, 
    {"field":"ingredients","gte":"bakepulver","lte":"bakepulver"}, 
    {"field":"ingredients","gte":"hvetemel","lte":"hvetemel"} 
] 

我不知道我想刪除的對象的索引,但我知道整個對象。即:

{"field":"ingredients","gte":"bakepulver","lte":"bakepulver"} 

我需要找到對象的指數它的全部內容(所有屬性),而不僅僅是fieldgtelte。如何用普通的JavaScript在數組中找到對象的索引?

+1

之間的所有東西都是獨一無二的這些對象? – brk

+1

你的對象可以有任何屬性是對象/數組本身? (嵌套) – trincot

+2

['findIndex'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex)! – Bergi

回答

2

您可以迭代數據,然後檢查鍵和每個鍵的長度(如果它具有相同的內容)。

var data = [{ "field": "ingredients", "gte": "egg", "lte": "egg" }, { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" }, { "field": "ingredients", "gte": "hvetemel", "lte": "hvetemel" }], 
 
    search = { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" }, 
 
    keys = Object.keys(search), 
 
    index = -1; 
 

 
data.some(function (a, i) { 
 
    if (Object.keys(a).length === keys.length && keys.every(function (k) { return a[k] === search[k]; })) { 
 
     index = i; 
 
     return true; 
 
    } 
 
}); 
 

 
console.log(index);

ES6

var data = [{ "field": "ingredients", "gte": "egg", "lte": "egg" }, { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" }, { "field": "ingredients", "gte": "hvetemel", "lte": "hvetemel" }], 
 
    search = { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" }, 
 
    keys = Object.keys(search), 
 
    index = data.findIndex(a => 
 
     Object.keys(a).length === keys.length && keys.every(k => a[k] === search[k])); 
 

 
console.log(index);

+0

僅僅爲了迭代而使用'Array.prototype.some'的原因是什麼? (而不是'Array.prototype.forEach') – zerkms

+2

如果找到第一個匹配的對象,它結束迭代。 'forEach'遍歷數組的所有項目。 –

+1

@NinaScholz @ninaScholz如果你想'某些'提早結束,你可能會想''返回true'。當前的代碼將返回最後一個匹配的索引。 – Dogbert

2

您可以使用Object.keys()Array.prototype.findIndex()Array.prototype.every()檢查,如果每個屬性名稱,值和對象屬性名.length是相等的。

let data = [ 
 
    {"field":"ingredients","gte":"egg","lte":"egg"}, 
 
    {"field":"ingredients","gte":"bakepulver","lte":"bakepulver"}, 
 
    {"field":"ingredients","gte":"hvetemel","lte":"hvetemel"} 
 
]; 
 

 
let props = {"field":"ingredients","gte":"bakepulver","lte":"bakepulver"}; 
 
let keys = Object.keys(props); 
 
let index = data.findIndex(o => keys.every(key => o[key] === props[key]) 
 
       && Object.keys(o).length === keys.length); 
 

 
console.log(index);

0

您可以使用此代碼爲您的問題

var array = [ 
    {"field":"ingredients","gte":"egg","lte":"egg"}, 
    {"field":"ingredients","gte":"bakepulver","lte":"bakepulver"}, 
    {"field":"ingredients","gte":"hvetemel","lte":"hvetemel"} 
]; 
var searchObject =  
    {"field":"ingredients","gte":"bakepulver","lte":"bakepulver"}; 

var indexOfSearchResult; 

for (var i = 0; i < array.length; i++) { 
    var checkEqualBool = true; 
    for (var key in array[i]) { 
     if(array[i][key]!=searchObject[key]){ 
      checkEqualBool = false; 
      break; 
     } 
    } 
    if(checkEqualBool){ 
     indexOfSearchResult = i; 
     break; 
    } 
} 
0

我用的比較對象的老方法,通過字符串化它。嘗試當我從你的意見進一步瞭解審查這一

var x = [{ 
 
    "field": "ingredients", 
 
    "gte": "egg", 
 
    "lte": "egg" 
 
}, { 
 
    "field": "ingredients", 
 
    "gte": "bakepulver", 
 
    "lte": "bakepulver" 
 
}, { 
 
    "field": "ingredients", 
 
    "gte": "hvetemel", 
 
    "lte": "hvetemel" 
 
}]; 
 
var control = { 
 
    "field": "ingredients", 
 
    "gte": "bakepulver", 
 
    "lte": "bakepulver" 
 
}; 
 

 
function getIndex(arr, key) { 
 
    var got = false, 
 
    result = -1; 
 
    arr.every(function(e, i) { 
 
    if (JSON.stringify(e) === JSON.stringify(key)) { 
 
     console.log('match found'); 
 
     result = i; 
 
     return false; 
 
    } 
 
    return true; 
 
    }) 
 
    return result; 
 
} 
 

 
console.log(getIndex(x, control));

+0

只有當屬性具有相同的順序時纔有效,這與對象無關。 –

+0

雅這是主要缺點。完全同意你@NinaScholz – RizkiDPrast

1

,你需要的只是這個特定的情況下,其中的對象包括3種給出的性能的解決方案。因此,我建議這個ES6解決方案:

var data = [ 
 
    { "field": "ingredients", "gte": "egg", "lte": "egg" }, 
 
    { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" }, 
 
    { "field": "ingredients", "gte": "hvetemel", "lte": "hvetemel" }]; 
 
var search = { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" }; 
 

 
var index = data.findIndex(
 
    a => a.field == search.field && a.gte == search.gte && a.lte == search.lte); 
 

 
console.log(index);

如果你沒有完全ES6支持,則:

var data = [ 
 
    { "field": "ingredients", "gte": "egg", "lte": "egg" }, 
 
    { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" }, 
 
    { "field": "ingredients", "gte": "hvetemel", "lte": "hvetemel" }]; 
 
var search = { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" }; 
 

 
var index = -1; 
 
data.some(function (a, i) { 
 
    if (a.field == search.field && a.gte == search.gte && a.lte == search.lte) 
 
     return index = i, true; 
 
}); 
 
console.log(index);

相關問題