2016-11-29 70 views
0

我有十二個項目的數組:在JavaScript操縱陣列通過切片

var arrToIterate = []; 
     arrToIterate = //Data from service to populate; 

陣列中的每個項目都有兩個字段:名稱和標題。

如果假設數組有名稱爲:

Scenario 1: 
[0]: Name: Iron 
[1]: Name: Steel 
[2]: Name: ContainsIron 
[3]: Name: ContainsSteel 
[4]: Name : Manganese 
[5]: Name: Magnesium 

我需要的輸出中:

[0]: Name: Iron 
[1]: Name: Steel 
[2]: Name : Manganese 
[3]: Name: Magnesium 



Scenario 2: 
If suppose the array has Names as : 
[0]: Name: Iron 
[1]: Name: Steel 
[2]: Name : Manganese 
[3]: Name: Magnesium 
I need the output as: 

[0]: Name: Manganese 
[1]: Name: Magnesium 

我想這樣做:

$.each(arrToIterate, function (element, index, arr) { 
            if (element.Name == "ContainsIron" || element.Name == "ContainsSteel") { 
             arr.splice(index, 1); 
            } 
           }); 

但第二種情況我沒有掌握。我如何實現這兩種場景的數組操作?

編輯:

如果數組包含 「ContainsIron」,然後ContainsIron需要被去除,同樣地,如果它包含ContainsSteel,然後ContainsSteel需要被去除。

否則

如果陣列犯規包含ContainsSteel,然後需要被去除。

如果陣列不包含ContainsIron,然後需要刪除。

+4

目前還不清楚什麼是真正的關係是 - 什麼定義哪些元素需要被刪除?此外,這是一個情況下,jQuery的弊大於利 - 只是一個簡單的for循環會很好。 –

+0

通過使用諸如'.filter'之類的東西來保留原始數組,以功能性的方式解決了問題,它使用'.filter'來返回已過濾項目的新數組。 – ioneyed

回答

1

這是一個簡單的版本。首先,你要做出一個刪除功能,刪除的東西,如果它在那裏,一個函數,它刪除了一個名爲金屬:

function removeIfExists(arr,name){ 

    // get the index of the entry: 
    for(var i in arr){ 
     if(arr[i].Name==name){ 
      // Got it! Rip it out: 
      arr.splice(i,1); 
      return true; 
     } 
    } 

    // Not in the array at all. 
    return false; 

} 

// Removes a metal from the given array 
function removeMetal(arr,name){ 

    // Try removing e.g. ContainsIron: 
    if(!removeIfExists(arr,"Contains"+name)){ 
     // ContainsIron/ ContainsSteel wasn't in there. Try removing 'Iron'/ 'Steel': 
     removeIfExists(arr,name); 
    } 

} 

這使得使用縮寫爲:

removeMetal(arrToIterate,"Iron"); 
removeMetal(arrToIterate,"Steel"); 

Here it is as a fiddle

1

你可以用香草Javascript來做到這一點,利用過濾功能:

var arrToIterate = [{ 
 
    name: 'Iron' 
 
}, { 
 
    name: 'Iron' 
 
}, { 
 
    name: 'Iron' 
 
}, { 
 
    name: 'Manganese' 
 
}, { 
 
    name: 'Iron' 
 
}, { 
 
    name: 'Iron' 
 
}, { 
 
    name: 'Iron' 
 
}, { 
 
    name: 'ContainsSteel' 
 
}]; 
 

 
filterArray(arrToIterate); 
 

 
function filterArray(arrToIterate) { 
 
    var containsSteel = false, 
 
    containsIron = false, 
 
    filteredArray; 
 
    arrToIterate.forEach(function(item) { 
 
    (item.name === 'ContainsSteel') ? containsSteel = true: null; 
 
    (item.name === 'ContainsIron') ? containsIron = true: null; 
 
    }); 
 

 
    console.log("Original Array"); 
 
    console.log(arrToIterate); 
 
    console.log("ContainsSteel " + containsSteel); 
 
    console.log("ContainsIron " + containsIron); 
 
    if (containsSteel) { 
 
    filteredArray = arrToIterate.filter(function(item) { 
 
     return !(item.name === 'ContainsSteel'); 
 
    }); 
 
    } 
 

 
    if (containsIron) { 
 
    filteredArray = filteredArray.filter(function(item) { 
 
     return !(item.name === 'ContainsIron'); 
 
    }); 
 
    } 
 

 
    if (!(containsIron)) { 
 
    filteredArray = filteredArray.filter(function(item) { 
 
     return !(item.name === 'iron'); 
 
    }) 
 
    } 
 

 
    if (!(containsSteel)) { 
 
    filteredArray = filteredArray.filter(function(item) { 
 
     return !(item.name === 'Steel'); 
 
    }) 
 
    } 
 
    console.log("Filtered array "); 
 
    console.log(filteredArray); 
 
    return filteredArray; 
 
};

0

如果你只是想你可以使用過濾器濾鏡陣列:

var array = [ 
    { name: 'Iron'}, 
    { name: 'Steel'}, 
    { name: 'Manganese'}, 
    { name: 'Magnesium'} 
]; 

var filtered = array.filter(function(item) { 
    return item.name === 'Iron'; 
}) 

不確定你的選擇標準是什麼,但你只需要定義t他們在過濾器回調的身體。

編輯

我看到你更新你的標準 - 所以你只需要相應地,因爲我覺得現在其他的反應已經表明修改過濾器回調。

1

由於缺乏一致性的「圍堵」規則(錳例如,沒有他們),我們需要定義什麼將不會顯示散列如果沒有一個包含規則它。 然後,我們可以掃描數組中的「包含」規則更新哈希,然後相應地過濾數組。

var arrToIterate = [ 
 
    { Name: 'Iron' }, 
 
    { Name: 'Steel' }, 
 
    { Name: 'ContainsIron' }, 
 
    { Name: 'Manganese' }, 
 
    { Name: 'Magnesium' } 
 
]; 
 

 
var result = arrToIterate.filter(function(metal) { 
 
    return metal.Name in this ? this[metal.Name] : true; // if there's an entry in the hash (this) use it. If not keep the item. 
 
}, arrToIterate.reduce(function(hash, metal) { // create the hash 
 
    if(metal.Name.indexOf('Contains') !== -1) { // if there's a contain rule 
 
    hash[metal.Name] = false; // remove the rule 
 
    hash[metal.Name.replace('Contains', '')] = true; // show the metal 
 
    } 
 

 
    return hash; 
 
}, { Iron: false, Steel: false })) // the baseline is false for every metal with a Contain rule 
 

 
console.log(result);