2016-11-29 110 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);