簡體   English   中英

將數組的 object 的數組與 javascript 中的另一個數組進行比較

[英]compare array of object of array with an another array in javascript

有兩個 arrays 一個帶有一個簡單數組,所有元素都具有 integer 值,另一個帶有帶有數組的對象數組(嵌套對象)。

需要比較兩個數組並刪除不相等的值。

 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]

將 checkedValue 與tier_agent_id進行比較 如果tier_agent_id和 checkedValue 不同,則刪除層 object

您可以使用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; }

另一種選擇,更長,但比它更具可讀性

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM