0

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 .

2
  • 4
    It's unclear what the relationship actually is - what defines which elements need to be removed? Also, this is a situation where jQuery does more harm than good - just a simple for loop would be fine. Commented Nov 29, 2016 at 6:35
  • Solved the problem in a functional manner by keeping your original array around by using things like .filter which return a new array of the filtered items. Commented Nov 29, 2016 at 6:41

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.