0

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

7
  • what about null? Commented Jun 15, 2021 at 11:18
  • dont consider that. do u have any solution for that as I'm new to this Commented Jun 15, 2021 at 11:19
  • What is the expected output? What will the output if only one tier_agent_id is present in the checkedValue? Commented Jun 15, 2021 at 11:20
  • Should it remain as it is or it is discarded in the final result? Commented Jun 15, 2021 at 11:21
  • if the value present in the checkedValue doesn't match the tier_agent_id then dont return that particular object Commented Jun 15, 2021 at 11:22

2 Answers 2

1

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;
}

Sign up to request clarification or add additional context in comments.

2 Comments

but why u added that css .as-console-wrapper ? i'm confused
Ignore it. It is just to get the full height of the output when you click Run code snippet?
1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.