By Rambabu Bommisetti


2018-02-13 13:43:13 8 Comments

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 = [?];

5 comments

@Tomasz BubaƂa 2018-02-13 13:51:01

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

@zb22 2018-02-13 13:52:59

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)

@Rambabu Bommisetti 2018-02-14 07:56:17

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] ....

@zb22 2018-02-15 10:05:45

you can do this then: jsfiddle.net/9f8hmdau/24

@digital-pollution 2018-02-13 13:55:47

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

@trichetriche 2018-02-13 13:55:05

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

@Akbar Badhusha 2018-02-13 13:54:16

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

Related Questions

Sponsored Content

36 Answered Questions

[SOLVED] Check if object is array?

  • 2011-01-23 18:53:04
  • mpen
  • 1117678 View
  • 1967 Score
  • 36 Answer
  • Tags:   javascript arrays

35 Answered Questions

[SOLVED] Compare two dates with JavaScript

57 Answered Questions

[SOLVED] How do I remove a particular element from an array in JavaScript?

  • 2011-04-23 22:17:18
  • Walker
  • 4221938 View
  • 5437 Score
  • 57 Answer
  • Tags:   javascript arrays

31 Answered Questions

[SOLVED] How do I return the response from an asynchronous call?

31 Answered Questions

[SOLVED] How do I remove a property from a JavaScript object?

51 Answered Questions

[SOLVED] How do I correctly clone a JavaScript object?

  • 2009-04-08 03:01:06
  • mindeavor
  • 1273584 View
  • 2181 Score
  • 51 Answer
  • Tags:   javascript

40 Answered Questions

[SOLVED] How do I check if an array includes an object in JavaScript?

25 Answered Questions

[SOLVED] How to append something to an array?

18 Answered Questions

[SOLVED] How do I empty an array in JavaScript?

  • 2009-08-05 09:08:39
  • amir
  • 1673019 View
  • 2199 Score
  • 18 Answer
  • Tags:   javascript arrays

53 Answered Questions

[SOLVED] How can I merge properties of two JavaScript objects dynamically?

Sponsored Content