简体   繁体   English

如何比较两个数组对象并从一个数组对象中删除匹配的对象

[英]How to compare two array objects and remove matched objects from one array object

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

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 : 要以编程方式获取字符串数组而不是对象数组,可以使用map

var seletedEmployees= [{name:"mohan"},{name:"ranga"}].map(emp => emp.name);

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)

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

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

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

var Employees= [{name:"Ram",htno:1245},{name:"mohan",htno:1246}, {name:"madhu",htno:1247},{name:"ranga",htno:1248}] 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 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)); }); var EmployeesdataAfterremoveSelected = Employees.filter(function(val,index){console.log(val.name)return!(seletedEmployees.map(function(e){return e.name;})。indexOf(val.name));; });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较两个对象数组,如果属性值匹配,则从第一个数组中删除对象 - Compare two array of objects and remove objects from first array if a property value is matched 从深度嵌套的对象数组中删除匹配的对象 - Remove matched object from deeply nested array of objects 根据属性值比较两个对象数组并返回匹配的 - Compare two array of objects based on a properties value and return the matched 比较两个具有对象的 arrays 并从第一个数组中删除重复项 - Compare two arrays having objects and remove duplicates from first array 比较两个数组并删除不匹配的对象 - compare two arrays and remove not matched objects 如何返回一个对象数组,返回与某些特定键匹配的对象:来自两个不同对象的值? - how to return an array of objects returning the objects that matched some specific key:values from two different objects? Javascript / underscore / lodash:在对象数组中比较对象并删除匹配的对象 - Javascript/underscore/lodash : Comparing object in array of objects and remove the matched object 比较两个对象,仅返回第一个对象的匹配值 - Compare two objects and return only the matched values from the first object 比较一组对象中的对象 - Compare objects in one array of objects 如何从jQuery中的对象数组中删除一个对象? - How to remove an object from an array of objects in jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM