简体   繁体   English

如何根据条件检查对象数组中的重复属性 javascript

[英]How to check the duplicates property in array of objects based on condition javascript

I have array of object arrayobj in which have to check property value of我有 object arrayobj数组,其中必须检查的属性值

code and type based on condition return statement no add or can add in javascript javascript中基于条件返回语句的codetype no addcan add

in arrobj for type-local, if type local and code values not reappearing in other object return ' can add ' else return 'no add'在 arrobj for type-local, if type local and code values not reappearing in other object返回“可以添加”,否则返回“不添加”

in arrobj for type-FL or FL Travel, if different type and code values reappearing in other object return ' can add '在 arrobj for type-FL or FL Travel, if different type and code values reappearing in other object返回 '可以添加'

in arrobj for type-FL or FL Travel if type and code both reappearing in object return ' no add '在 arrobj for type-FL or FL Travel if type and code both reappearing in object返回“不添加

How to check above condition in javascript,如何在javascript中查看以上情况,

have tried code without checking property type , got stuck在不检查属性type的情况下尝试了代码,卡住了

function checkValues(arrob){
 const checkcode = arrob.map(elem => elem.code);
if (arrob.length !== new Set(checkcode).size) {
 return "no add";
} else {
 return "can add"
}
}
checkValues(arrobj1);
checkValues(arrobj2);
checkValues(arrobj3);

sample objects for scenario

var  arrobj1=[
  {id:1, name: "ram", code: "NIXZ", type: "Local"},
  {id:2, name: "abey", code: "ABCN", type: "FI"},
  {id:3, name: "jaz", code: "ABCN", type: "FI Travel"}
]
var  arrobj2=[
  {id:1, name: "ram", code: "NIXZ", type: "Local"},
  {id:4, name: "zain", code: "NIXZ", type: "FI"},
  {id:2, name: "abey", code: "ABCN", type: "FI"},
  {id:3, name: "jaz", code: "ABCNE", type: "FI Travel"}
]
var  arrobj3=[
  {id:1, name: "ram", code: "NIXZ", type: "Local"},
  {id:4, name: "sam", code: "ABCN", type: "FI"},
  {id:2, name: "abey", code: "ABCN", type: "FI"},
  {id:3, name: "jaz", code: "ABCNE", type: "FI Travel"}
]

Expected Result预期结果

//for arrobj1 [type-FL or FL Travel, different type and same code appears]
can add

//for arrobj2 [type local, same code appears] 
no add

//for arrobj3 [type-FL or FL Travel, same type and same code appears] 
no add

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array. map() 方法创建一个新数组,其中填充了对调用数组中每个元素调用提供的 function 的结果。

const rawArray = [arrobj1 , arrobj2 , arrobj3]
const hasDups = (objArray) => {
    objArray.ForEach(el => {
        if (objArray.Includes(el.type))
            return true;
        return false;
    });    
}
const newArray = rawArray.map(el => !hasDups(el));

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

相关问题 根据 javascript 中的条件检查对象数组中多个字段中的重复项 - check for duplicates in multiple fields in array of objects based on condition in javascript 如何根据 ID 检查对象数组并在 JavaScript 中添加新属性 - How to check array of objects based on ID and add new property in JavaScript 根据 88222995402888 属性值从 javascript 对象数组中删除重复项 - Remove duplicates from javascript objects array based on object property value 从JavaScript中的对象数组中删除重复项并基于布尔属性进行过滤 - Removing duplicates and filtering based on a boolean property from array of objects in JavaScript javascript根据条件从对象的嵌套数组返回属性值 - javascript return property value from nested array of objects based on condition 根据属性值删除对象数组中的重复项 - Removing Duplicates in Array of Objects based on Property Values 如何根据JavaScript中的值检查对象是否在数组中? - How to check if objects are in an array based on a value in JavaScript? 检查javascript数组对象属性 - Check javascript array of objects property Javascript 数组 - 根据另一个属性删除重复项 - Javascript Array - Remove duplicates based on another property 如何用不同的值和基于对象属性的条件填充数组? - How fill the array with distinct values and with condition based on property of objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM