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

6 comments

@Rambabu Bommisetti 2018-03-06 13:53:28

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

@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

26 Answered Questions

[SOLVED] How to append something to an array?

55 Answered Questions

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

34 Answered Questions

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

34 Answered Questions

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

54 Answered Questions

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

  • 2009-04-08 03:01:06
  • mindeavor
  • 1355378 View
  • 2312 Score
  • 54 Answer
  • Tags:   javascript clone

38 Answered Questions

[SOLVED] Check if object is array?

  • 2011-01-23 18:53:04
  • mpen
  • 1167636 View
  • 2076 Score
  • 38 Answer
  • Tags:   javascript arrays

63 Answered Questions

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

  • 2011-04-23 22:17:18
  • Walker
  • 4586645 View
  • 5798 Score
  • 63 Answer
  • Tags:   javascript arrays

35 Answered Questions

[SOLVED] Compare two dates with JavaScript

40 Answered Questions

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

18 Answered Questions

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

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

Sponsored Content