0

My Code Scenario is:

var Employees= [{name:"Ram",htno:1245},{name:"mohan",htno:1246},
{name:"madhu",htno:1247},{name:"ranga",htno:1248}]

var seletedEmployees= [{name:"mohan"},{name:"ranga"}];

var employeesdataAfterremoveSelected = [?];
3
  • 2
    What you've tried? Commented Feb 13, 2018 at 13:43
  • Try this Employees.filter(emp => seletedEmployees.every(semp => semp.name !== emp.name)) Commented Feb 13, 2018 at 13:49
  • Can you make the question more clear? Right now its just code Commented Feb 13, 2018 at 13:50

6 Answers 6

1

You can store selected employees names in an array and then filter Employees array and check if employee's name is in this array:

var employees= [{name:"Ram",htno:1245},{name:"mohan",htno:1246},{name:"madhu",htno:1247},{name:"ranga",htno:1248}]
var selectedEmployees= ["mohan","ranga"];
var result = employees.filter(emp => selectedEmployees.includes(emp.name));
console.log(result);

To programatically get array of strings instead array of objects, you can use map:

var seletedEmployees= [{name:"mohan"},{name:"ranga"}].map(emp => emp.name);
Sign up to request clarification or add additional context in comments.

Comments

1

From the code you have given above i think this might work

$.each(student, function(key, value){
   if(matchedvalues.indexOf(value.name) < 0)
     {
       employeesdataAfterremoveSelected.push(value.name);
     }
})

Comments

1

Here is a one liner, decomposed to explain :

// Start by filtering the first array on a condition.
employeesdataAfterremoveSelected = Employees.filter(
  // Map the array of selected employees to only return the name
  e => seletedEmployees.map(_e => _e.name)
    // use the includes function to check if the name is in the array
    .includes(e.name)
);

In one line :

employeesdataAfterremoveSelected = Employees.filter(e => seletedEmployees.map(_e => _e.name).includes(e.name));

Comments

1

You can use the filter method, something like below (not tested)

var Employees = [{name:"Ram",htno:1245}, {name:"mohan",htno:1246}]

var SelectedEmployess = [{name:"Ram",htno:1245}]

// filter the items from the invalid list, out of the complete list
    var employeesdataAfterremoveSelected = Employees.filter((item.name) => {
    return !SelectedEmployess.has(item.name);
})

// get a Set of the distinct, valid items
var validItems = new Set(employeesdataAfterremoveSelected);

Comments

1

You can try this:

var Employees = [{name:"Ram",htno:1245},{name:"mohan",htno:1246},
    {name:"madhu",htno:1247},{name:"ranga",htno:1248}] 

var seletedEmployees = [{name:"mohan"},{name:"ranga"}];

var employeesdataAfterremoveSelected = Employees.filter(name => {
  return (name.name !== seletedEmployees[0].name && name.name !== seletedEmployees[1].name)
}) 

console.log(employeesdataAfterremoveSelected)

1 Comment

if i want to remove one more element instead of var seletedEmployees = [{name:"mohan"},{name:"ranga"}]; i want to delete var seletedEmployees = [{name:"mohan"},{name:"ranga"},{name:"Rambabu"}]; how can I achieve without hard coded inde values as [0] , [1] ....
0

var Employees= [{name:"Ram",htno:1245},{name:"mohan",htno:1246}, {name:"madhu",htno:1247},{name:"ranga",htno:1248}]

var seletedEmployees= [{name:"mohan"},{name:"ranga"}];

var employeesdataAfterremoveSelected = Employees.filter(function(val,index) { console.log(val.name) return !(seletedEmployees.map(function(e) { return e.name; }).indexOf(val.name)); });

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.