根据JavaScript中的条件,在一系列对象中的多个字段中检查重复项

发布于 2025-01-19 19:41:37 字数 1132 浏览 1 评论 0 原文

我有一个对象数组,其中基于

以下条件的重复返回true或false,

如果如果类型为local local locatik code true

如果类型在或旅行中具有重复代码对象数组中的值

如果 对象数组中的值

code>在

var  objarray1=[
  {id:1, name: "ram", code: "NIXZ", type: "Local"},
  {id:2, name: "abey", code: "ABCN", type: "IN"},
  {id:3, name: "jaz", code: "ABCN", type: "IN Travel"}
]

Expected Result 
// since type-IN or IN travel and code has duplicate
false

// this code not working for above conditions
function checkArrayObject(arrobj){
   
  const getLocal = arrobj.filter(e=>e.type=="local");
  const checklocalcode = arrobj.filter(e=>e.type=="local").map(i=>i.code);

  const getIN = arrobj.filter(e=>e.type=="IN");
  const checkINcode = arrobj.filter(e=>e.type=="IN").map(i=>i.code);
  var result = arrobj.forEach(e=>{
    if(getLocal.length === checklocalcode.length) {
      return true;
    } else {
     return false;
    }
    if(getIN.length === checkINcode.length){
     return true;
    } else {
     return false;
    }
     
   }
  })
}

I have array of object in which return true or false based on duplicates

based on below conditions return true or false in javascript

if the if type is local has duplicate code value in object array return true

if the if type is IN or IN travel has duplicate code value in object array return false

if the if same type IN and has same code value in object array return true

if the if same type IN Travel and has same code value in object array return true

var  objarray1=[
  {id:1, name: "ram", code: "NIXZ", type: "Local"},
  {id:2, name: "abey", code: "ABCN", type: "IN"},
  {id:3, name: "jaz", code: "ABCN", type: "IN Travel"}
]

Expected Result 
// since type-IN or IN travel and code has duplicate
false

// this code not working for above conditions
function checkArrayObject(arrobj){
   
  const getLocal = arrobj.filter(e=>e.type=="local");
  const checklocalcode = arrobj.filter(e=>e.type=="local").map(i=>i.code);

  const getIN = arrobj.filter(e=>e.type=="IN");
  const checkINcode = arrobj.filter(e=>e.type=="IN").map(i=>i.code);
  var result = arrobj.forEach(e=>{
    if(getLocal.length === checklocalcode.length) {
      return true;
    } else {
     return false;
    }
    if(getIN.length === checkINcode.length){
     return true;
    } else {
     return false;
    }
     
   }
  })
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千鲤 2025-01-26 19:41:37
const filter = (arr) => {
  for (let i = 0; i < arr.length; i++)
    for (let j = 0; j < arr.length; j++)
      if (i !== j && arr[i].code == arr[j].code)
        return false;

  return true;
};
const filter = (arr) => {
  for (let i = 0; i < arr.length; i++)
    for (let j = 0; j < arr.length; j++)
      if (i !== j && arr[i].code == arr[j].code)
        return false;

  return true;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文