user1400915
user1400915

Reputation: 1943

Manipulating array in javascript by slicing

I have an array of twelve items :

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

Each item in the array has two fields : Name and title.

If suppose the array has Names as :

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

I need the ouput as :

[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

I am trying to do in this way :

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

But I am not getting the handle the second scenario . How do I achieve array manipulation for both the scenarios?

Edit :

If the array contains "ContainsIron" , then ContainsIron needs to be removed ,similarly if it contains ContainsSteel , then ContainsSteel needs to be removed.

Else

If array doesnt contain ContainsSteel , then Steel needs to be removed .

If array doesnt contain ContainsIron, then Iron needs to be removed .

Upvotes: 0

Views: 52

Answers (4)

Ori Drori
Ori Drori

Reputation: 191986

Because of the lack of consistency of the "Contain" rules (Manganese for example, doesn't have them), we need to define a hash of what won't be displayed if there isn't a contain rule for it. Afterwards we can scan the array for for "Contain" rules update the hash, and then just filter array accordingly.

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);

Upvotes: 1

Luke Briggs
Luke Briggs

Reputation: 3789

Here's a simple version. First, you'll want to make a remove function which removes something if it's in there, and a function which removes a named metal:

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);
    }

}

That leaves the usage as just:

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

Here it is as a fiddle

Upvotes: 1

nikjohn
nikjohn

Reputation: 21850

You can just use vanilla Javascript to do this, using the filter function:

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;
};

Upvotes: 1

Mark Williams
Mark Williams

Reputation: 2308

If you just want to filter the array you can use filter:

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

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

Not sure what your selection criteria is, but you just need to define these in the body of the filter callback.

Edit

I see you updated your criteria - so you would just have to amend the filter callback accordingly as I think other responders have now indicated.

Upvotes: 0

Related Questions