0

I have array of objects, I am trying to loop through that array and if I find found a match, I wanna slice that object..

var object = [
  {
    "Name": 'Kshitij',
    "LastName": 'Rangari',
    "CountryBorn": 'India',
    "CountryStay": 'USA'
  },

  {
    "Name": 'Pratik',
    "LastName": 'Rangari',
    "CountryBorn": 'India',
    "CountryStay": 'Canada'
  },

  {
    "Name": 'Pratibha',
    "LastName": 'Rangari',
    "CountryBorn": 'India',
    "CountryStay": 'India'
  },

  {
    "Name": 'Ankita',
    "LastName": 'Raut',
    "CountryBorn": 'India',
    "CountryStay": 'Australia'
  },

  {
    "Name": 'Wayne',
    "LastName": 'Rooney',
    "CountryBorn": 'UK',
    "CountryStay": 'UK'
  }

]

console.log(object);

object.forEach(function(x){
  if (x.Name==='Kshitij'){

  }
})

object.map (obj =>{

  obj.AllFirstName = obj['Name'];
  console.log(obj['AllFirstName']);

})

console.log('------------------------------')

console.log(object); 

I wanna loop through Object and want to find if Name === 'Kshitij' and Name ==='Pratik', I want to remove those objects from the array.

How would I do this ?

curious_debugger
  • 299
  • 2
  • 8
  • 23
  • [`arr.forEach(function callback(currentValue, **index**, array) ...)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach). Also while calling an array `object` is technically correct it is a bit misleading here. – ASDFGerte Aug 12 '17 at 16:15
  • 1
    This correct term is not "slicing". It is "filtering out". –  Aug 13 '17 at 04:48

3 Answers3

3

You could use return a filtered array, which filters out all the given values for a given key of the objects in the array.

const without = (key, values) => object => !values.includes(object[key]);

var array = [{ Name: 'Kshitij', LastName: 'Rangari', CountryBorn: 'India', CountryStay: 'USA' }, { Name: 'Pratik', LastName: 'Rangari', CountryBorn: 'India', CountryStay: 'Canada' }, { Name: 'Pratibha', LastName: 'Rangari', CountryBorn: 'India', CountryStay: 'India' }, { Name: 'Ankita', LastName: 'Raut', CountryBorn: 'India', CountryStay: 'Australia' }, { Name: 'Wayne', LastName: 'Rooney', CountryBorn: 'UK', CountryStay: 'UK' }];

console.log(array.filter(without('Name', ['Kshitij', 'Pratik'])));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • You're taking the "requirement" stated as "remove" too seriously, which is leading you to the horrible suggestion to use `splice`, which then forces you to work backwards, or turn other somersaults. Furthermore, your logic seems to only remove the first occurrence. Not to mention that the whole thing is a dup of about eighteen other questions. –  Aug 13 '17 at 05:03
  • @torazaburo, right, i take words seriously. and i have misread the question, but what the op wants was only later clear. anyway, now with filtering and using a staight fprward approach. btw, the dupe target is horrible. – Nina Scholz Aug 13 '17 at 08:00
  • Much better. Wrt the dup target, I am not happy about it either, but this question falls into the category of so incredibly basic that there doesn't seem to be a clean dup, sort of like a question about how to add two numbers in JS. –  Aug 13 '17 at 11:34
2

Use the builtin filter to remove all items that match a certain predicate. Then set the original object to the filtered list.

var namesToRemove = ['Kshitij', 'Pratik'];
object = object.filter(e => !namesToRemove.includes(e.Name))
Julian Zucker
  • 564
  • 4
  • 13
1

Use filter() function to get a new array with the objects removed - see demo below:

var object=[{"Name":'Kshitij',"LastName":'Rangari',"CountryBorn":'India',"CountryStay":'USA'},{"Name":'Pratik',"LastName":'Rangari',"CountryBorn":'India',"CountryStay":'Canada'},{"Name":'Pratibha',"LastName":'Rangari',"CountryBorn":'India',"CountryStay":'India'},{"Name":'Ankita',"LastName":'Raut',"CountryBorn":'India',"CountryStay":'Australia'},{"Name":'Wayne',"LastName":'Rooney',"CountryBorn":'UK',"CountryStay":'UK'}];

var result = object.filter(function(e){
  return e.Name !== 'Kshitij' && e.Name !=='Pratik'
});

console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}
kukkuz
  • 41,512
  • 6
  • 59
  • 95