Reputation: 872
If in Input2 Products and Roles object required
key is false
, then Products and Roles
should be removed from input1.
If any one required is true
, Products and Roles
should not be removed.
In input2 if there is an object other than Product
or Roles
, then only attributes in input1 should be shown otherwise it should be removed.
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},
];
expected output
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);
Upvotes: 2
Views: 137
Reputation:
Your code was not wrong but you have forgotten something..., check the edits in your code snippet below and run the snippet.
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);
Upvotes: 0
Reputation: 433
Following your requirements, the code below can solve your problem.
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);
Upvotes: 0
Reputation: 92
This code is slightly more generic than the others, it will search through your input1 array, and only return items where the name doesn't include any of the keys from input2, or if it finds matches, will include them if any found keyword is "required". It uses the underscorejs library.
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/[email protected]/underscore-min.js"></script>
Upvotes: 0
Reputation: 10193
You can get filtered input2
based on each input1
name and decide to leave or remove as follows.
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);
Upvotes: 2
Reputation: 693
We can filter input2 elements and get those has key of 'Products' or 'Roles' and not required
.
If the length of result was 2 so condition is met and input1 is elements of input1 which has not name of 'Products and Roles'
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)
notice that keys in input2 must be unique.
Upvotes: 0