javascript - 比较后如何在对象数组中打印额外的对象?
问题描述
const array1 = [
{id: 1, Q_type: "AL"},
{id: 2, Q_type: "BL"},
{id: 3, Q_type: "CL"},
{id: 4, Q_type: "DL"}
]
const array2 = [
{id: 2, Q_type: "BL"},
{id: 3, Q_type: "CL"},
{id: 4, Q_type: "DL"}
]
const arrAfterComparison = array1.filter(val => !array2.includes(val))
我正在尝试比较array1
并array2
获取这两个数组中都不存在的对象
预期产出
arrAfterComparison = [{id:1,Q_type:"AL"}]
解决方案
使用Array.some()
内部Array.filter()
方法回调。
const array1 = [
{id: 1, Q_type: "AL"},
{id: 2, Q_type: "BL"},
{id: 3, Q_type: "CL"},
{id: 4, Q_type: "DL"}
]
const array2 = [
{id: 2, Q_type: "BL"},
{id: 3, Q_type: "CL"},
{id: 1, Q_type: "DL"}
]
const output = array1.filter(item => !array2.some(a => a.Q_type === item.Q_type))
console.log(output);
推荐阅读
- java - 用于将两个(证书和证书密钥)文件作为多部分表单数据发送的 HTTP 发布请求的 Java 代码
- php - [YAJRA-DATATABLE]在急切加载多对多关系时无法获取枢轴对象
- php - Lumen JWT Authentication 过关
- android - 如何修复“会话‘应用’:安装失败。”
- asp.net - 哪种方式适合在没有 XSLT 的情况下使用 ASP.NET 控制台应用程序进行动态 XML 转换?
- python - 如何从数组中弹出整个项目?
- selenium - 如何使用硒单击两个选项卡中的一个链接?
- regex - 杨正则表达式
- moodle - Scorm 包仅标记为“完成”而不是“通过”
- windows - Spark-submit 不像 Intellij 那样运行代码