javascript - 将数组对象的数组与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
解决方案
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;
}
推荐阅读
- google-colaboratory - 如何在 colaboratory(colab research)中更改字体大小?
- python - 龙卷风:[SSL:CERTIFICATE_VERIFY_FAILED]
- c++ - 为什么 QDBMP 无法写入 128*128 图像?
- python - Dfango 函数视图 VS 类 RedirectView。如何让类视图接受参数
- python - 使用日志输出终止 Python 多线程程序
- go - v.Elem() 与 Indirect(v) 相比,传入 reflect.New(Type) 的结果
- php - 更改数组内的值
- typescript - 重载不是类型检查主体
- puppet-enterprise - Puppet Enterprise "puppet job run Unknown Puppet subcommand 'job'" although documentation indicates this is available
- variables - Unified name for the national identification number