比较2个对象数组并过滤结果数组

时间:2020-10-22 10:37:04

标签: javascript arrays object

如果在Input2产品和角色对象中,required键为false,则应从输入1中删除Products and Roles

如果要求的任何一项是true,则不应删除Products and Roles

在input2中,如果存在ProductRoles以外的对象,则仅显示input1中的属性,否则应将其删除。

input1 = [
    { name: "Basic Details", description: "abc" },
    { name: "Products and Roles", value: "def" },
    { name: "Attributes", value: "ghi" }
];
    
input2 = [
    {key: "Product", type: "", value: "", required: false, mandatory: false },
    { key: "Roles", type: "", value: "", required: false, mandatory: false },
    { key: "sad", type: "text", value: "", required: false, mandatory: false},
];

预期产量

input1 = [
    { name: "Basic Details", description: "abc" },
    { name: "Attributes", value: "ghi" }
];

input2.filter(elm => {
    if(elm.key === 'Product' && !elm.required) {
        input1.filter(el => el.name !== 'Products & Roles');
    }
});

console.log(input1);

5 个答案:

答案 0 :(得分:2)

您可以根据每个input2的名称来过滤input1并决定如下离开或删除。

let input1 = [
  { name: "Basic Details", description: "abc" },
  { name: "Products and Roles", value: "def" },
  { name: "Attributes", value: "ghi" }
];

const input2 = [
  {key: "Product", type: "", value: "", required: false, mandatory: false },
  { key: "Roles", type: "", value: "", required: false, mandatory: false },
  { key: "sad", type: "text", value: "", required: false, mandatory: false},
];

const output = input1.filter((item) => {
  const matched = input2.filter((item2) => item.name.includes(item2.key));
  return matched.length === 0 || matched.some((item2) => item2.required);
});
console.log(output);

答案 1 :(得分:0)

按照您的要求,下面的代码可以解决您的问题。

input1 = [{
    name: "Basic Details",
    description: "abc"
  },
  {
    name: "Products and Roles",
    value: "def"
  },
  {
    name: "Attributes",
    value: "ghi",
  },
]

input2 = [{
    key: "Product",
    type: "",
    value: "",
    required: false,
    mandatory: false
  },
  {
    key: "Roles",
    type: "",
    value: "",
    required: false,
    mandatory: false
  },
  {
    key: "sad",
    type: "text",
    value: "",
    required: false,
    mandatory: false
  },
]

let productIndex = input2.findIndex(ele => {
  return ele.key === 'Product';
});

let rolesIndex = input2.findIndex(ele => {
  return ele.key === 'Roles';
});

let otherIndex = input2.findIndex(ele => {
  return ele.key !== 'Product' && ele.key !== 'Roles';
})

if (productIndex !== -1 && rolesIndex !== -1 && input2[productIndex].required === false && input2[rolesIndex].required === false) {
  input1 = input1.filter(el => el.name !== 'Products and Roles');
}

if (otherIndex === -1) {
  input1 = input1.filter(el => el.name !== 'Attributes');
}

console.log(input1);

答案 2 :(得分:0)

我们可以过滤input2元素并获取has key of 'Products' or 'Roles' and not required

如果结果的长度为2,则满足条件,并且input1是has not name of 'Products and Roles'的input1元素

 input1 = [
        { name: "Basic Details", description: "abc" },
        { name: "Products and Roles", value: "def" },
        { name: "Attributes", value: "ghi", },
    ]

input2 = [
    {key: "Product", type: "", value: "", required: false, mandatory: false },
    { key: "Roles", type: "", value: "", required: false, mandatory: false },
    { key: "sad", type: "text", value: "", required: false, mandatory: false},
]

if(input2.filter(elm => {
    return (elm.key === 'Product' || elm.key === 'Roles' && !elm.required)
}).length  == 2){
  input1 = input1.filter(elm => elm.name!='Products and Roles')
}
console.log(input1)

请注意,input2中的键必须唯一。

答案 3 :(得分:0)

您的代码没错,但是您忘记了某事... ,请检查下面代码段中的修改,然后运行该代码段。

input1 = [
    { name: "Basic Details", description: "abc" },
    { name: "Products and Roles", value: "def" },
    { name: "Attributes", value: "ghi" }
];
    
input2 = [
    { key: "Product", type: "", value: "", required: false, mandatory: false },
    { key: "Roles", type: "", value: "", required: false, mandatory: false },
    { key: "sad", type: "text", value: "", required: false, mandatory: false},
];
input2.filter(elm => {
    if(['Product','Roles'].includes(elm.key) && !elm.required) {
        input1 = input1.filter(el => el.name !== 'Products and Roles');
    }
});

console.log(input1);

答案 4 :(得分:0)

此代码比其他代码更具通用性,它将搜索您的input1数组,并且仅返回名称不包括input2中的任何键,或者找到匹配项的项(如果有)发现关键字为“必填”。它使用underscorejs库。

 input1 = [
        { name: "Basic Details", description: "abc" },
        { name: "Products and Roles", value: "def" },
        { name: "Attributes", value: "ghi", },
    ]

input2 = [
    { key: "Product", type: "", value: "", required: false, mandatory: false },
    { key: "Roles", type: "", value: "", required: false, mandatory: false },
    { key: "sad", type: "text", value: "", required: false, mandatory: false},
]

let requiredItems = _.filter(input1, function(item) {
  let words = item.name.split(' ');
  
  let filters = _.filter(input2, function(filter) {
        let found = _.find(words, function(word) {
        return (filter.key.includes(word) || word.includes(filter.key));
      });
            
      if(found){
        return filter;
      }
  });
    
  if(!filters || filters.length == 0){
    return item;
  }

  if(_.findWhere(filters, {required: true})){
    return item;
  }
});

console.log(requiredItems);
<script src="//cdn.jsdelivr.net/npm/underscore@1.11.0/underscore-min.js"></script>