javascript - 查找仅在 array1 javascript 中可用的对象
问题描述
这里我有两个数组。我想获取仅在 Array1 中可用的值。一些场景
- 如果两者都有共同的项目,则返回空。
- 如果两者都没有任何共同项目,则返回 array1 的所有项目。
- 如果 array1 有 item1 在数组 2 中不可用,则仅返回 item1
var Array1 = [{
id : 1,
name : 'item1'
}, {
id : 2,
name : 'item2'
}];
var Array2 = [{
id : 1,
name : 'item1',
expiredate : 2022
},
{ id : 3,
name : 'item3',
expiredate : 2022
}
];
// now want a result array3 which contains the item only available in array1. for above examplle item2
解决方案
你想要的只是array1
从array2
-
var array1 = [{
id: 1,
name: 'item1'
}, {
id: 2,
name: 'item2'
}];
var array2 = [{
id: 1,
name: 'item1',
expiredate: 2022
},
{
id: 3,
name: 'item3',
expiredate: 2022
}
];
var array3 = array1.filter(b =>
!array2.some(s => s.id === b.id && s.name === b.name));
console.log(array3);
推荐阅读
- visual-studio-2017 - 完全卸载 Visual Studio 2017 Enterprise,就像从未安装过一样
- html - 源 HTML5 视频元素的类型属性的 AV1 编解码器字符串
- delphi - 如何为对象实例创建新方法,例如 VCL TPanel
- database - 哪个数据库更适合用于电子窗口构建
- react-native - 反应本机代码推送未将应用程序更新到最新推送的版本
- c++ - 何时在迭代器上使用协程?
- javascript - Jest 快照测试忽略子样式
- css - 圆圈中的文字有响应
- firebase - Firebase 托管重定向到 Function + Angular Web App
- spring-boot - Spring boot + Logback-logstash appender:找不到名为的appender