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 ?

2
  • arr.forEach(function callback(currentValue, **index**, array) ...). Also while calling an array object is technically correct it is a bit misleading here. Commented Aug 12, 2017 at 16:15
  • 1
    This correct term is not "slicing". It is "filtering out". Commented Aug 13, 2017 at 4:48

3 Answers 3

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

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

3 Comments

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.
@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.
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.
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))

1 Comment

"I want to remove those objects from the array." .filter() returns a new array
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;}

2 Comments

"I want to remove those objects from the array." .filter() returns a new array
yup, edited answer to reflect that...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.