首页 > 解决方案 > 将数组对象的数组与javascript中的另一个数组进行比较

问题描述

有两个数组,一个是一个简单数组,所有元素都具有整数值,另一个是带有数组的对象数组(嵌套对象)。

需要比较两个数组并删除不相等的值。

 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]

如果和 checkValue 不相同,则将checkValue 与tier_agent_id删除层对象进行比较tier_agent_id

标签: javascriptarrays

解决方案


您可以使用mapfilter轻松实现此结果。

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


推荐阅读