Reputation: 107
have two arrays one with a simple array with all the elements have integer value and another one with array of objects with an array (nested object).
need to compare both the array and remove the value which is not equilant.
let userValue = [
{
userName: 'Abby Jerin',
tiers: [
{ tier_name: 'Colorado', errors: [], tier_agent_id: '115867' },
{ tier_name: 'MidSouth', errors: [], tier_agent_id: '115897' },
null,
],
},
{
userName: 'Alvin Lu',
tiers: [
{
tier_name: 'Frisco West',
errors: ['is publish disabled'],
tier_agent_id: '111257',
},
{
tier_name: 'MidSouth',
errors: ['is publish disabled'],
tier_agent_id: '116526',
},
null,
],
},
{
userName: 'Alfie Gonzalez',
tiers: [
{
tier_name: 'Hillsboro',
errors: ['is publish disabled'],
tier_agent_id: '111481',
},
{
tier_name: 'MidSouth',
errors: ['is publish disabled'],
tier_agent_id: '116527',
},
null,
],
},
{
userName: 'Amanda Prather',
tiers: [
{ tier_name: 'South King County', errors: [], tier_agent_id: '111506' },
{ tier_name: 'Dallas', errors: [], tier_agent_id: '114530' },
{
tier_name: 'Cypress Champion Forest',
errors: [],
tier_agent_id: '114532',
},
null,
],
},
]
let checkedValue = [115867, 115897, 111506, 114530, 114532]
compare checkedValue with the tier_agent_id
remove the tiers object if the tier_agent_id
and checkedValue are not same
Upvotes: 0
Views: 70
Reputation: 33
Another option, longer, but a little more readable than it does
const isChecked = value => checkedValue.some(item => item === value);
const hasChildren = tier => tier !== null && isChecked(Number(tier.tier_agent_id));
const filtered = userValue.filter(e =>
e.tiers.some(tier => hasChildren(tier)));
console.log(filtered);
let userValue = [
{
userName: 'Abby Jerin',
tiers: [
{ tier_name: 'Colorado', errors: [], tier_agent_id: '115867' },
{ tier_name: 'MidSouth', errors: [], tier_agent_id: '115897' },
null,
],
},
{
userName: 'Alvin Lu',
tiers: [
{
tier_name: 'Frisco West',
errors: ['is publish disabled'],
tier_agent_id: '111257',
},
{
tier_name: 'MidSouth',
errors: ['is publish disabled'],
tier_agent_id: '116526',
},
null,
],
},
{
userName: 'Alfie Gonzalez',
tiers: [
{
tier_name: 'Hillsboro',
errors: ['is publish disabled'],
tier_agent_id: '111481',
},
{
tier_name: 'MidSouth',
errors: ['is publish disabled'],
tier_agent_id: '116527',
},
null,
],
},
{
userName: 'Amanda Prather',
tiers: [
{ tier_name: 'South King County', errors: [], tier_agent_id: '111506' },
{ tier_name: 'Dallas', errors: [], tier_agent_id: '114530' },
{
tier_name: 'Cypress Champion Forest',
errors: [],
tier_agent_id: '114532',
},
null,
],
},
]
let checkedValue = [115867, 115897, 111506, 114530, 114532]
const isChecked = value => checkedValue.some(item => item === value);
const hasChildren = tier => tier !== null && isChecked(Number(tier.tier_agent_id));
const filtered = userValue.filter(e =>
e.tiers.some(tier => hasChildren(tier)));
console.log(filtered);
Upvotes: 1
Reputation: 25408
You can easily achieve this result using map and filter.
userValue.map((obj) => ({
...obj,
tiers: obj.tiers.filter(o => o && checkedValue.includes(Number(o.tier_agent_id))),
}))
let userValue = [{
userName: "Abby Jerin",
tiers: [{
tier_name: "Colorado",
errors: [],
tier_agent_id: "115867"
},
{
tier_name: "MidSouth",
errors: [],
tier_agent_id: "115897"
},
null,
],
},
{
userName: "Alvin Lu",
tiers: [{
tier_name: "Frisco West",
errors: ["is publish disabled"],
tier_agent_id: "111257",
},
{
tier_name: "MidSouth",
errors: ["is publish disabled"],
tier_agent_id: "116526",
},
null,
],
},
{
userName: "Alfie Gonzalez",
tiers: [{
tier_name: "Hillsboro",
errors: ["is publish disabled"],
tier_agent_id: "111481",
},
{
tier_name: "MidSouth",
errors: ["is publish disabled"],
tier_agent_id: "116527",
},
null,
],
},
{
userName: "Amanda Prather",
tiers: [{
tier_name: "South King County",
errors: [],
tier_agent_id: "111506"
},
{
tier_name: "Dallas",
errors: [],
tier_agent_id: "114530"
},
{
tier_name: "Cypress Champion Forest",
errors: [],
tier_agent_id: "114532",
},
null,
],
},
];
let checkedValue = [115867, 115897, 111506, 114530, 114532];
const result = userValue.map((obj) => ({
...obj,
tiers: obj.tiers.filter(o => o && checkedValue.includes(Number(o.tier_agent_id))),
}));
console.log(result);
.as-console-wrapper {
max-height: 100% !important;
top: 0;
}
Upvotes: 1