javascript - 基于真标志比较javascript中的两个对象
问题描述
我有一个这样的对象
const values = [
{firstValue: "Michael", secondValue: "Malkovich"},
{firstValue: "John", secondValue: "Doe"},
{firstValue: "James", secondValue: "Doe"}
];
还有一个像这样的物体
const incomingValues = {
Michael: true;
John: false;
James: true;
};
基于这两个对象,我需要制作另一个基于真或假过滤值的对象,我的最终对象必须是这样的
const filteredValues = [
{firstValue: "Michael", secondValue: "Malkovich"},
{firstValue: "James", secondValue: "Doe"}
];
你看没有约翰了,因为它是假的。我什至不知道如何比较这两个值。
解决方案
您可以使用filter
Array 对象中的函数:
const filteredValues = values.filter(({ firstValue }) => incomingValues[firstValue]);
推荐阅读
- python - “TypeError:在进行 numpy 点积时,不能将序列乘以‘float’类型的非整数”
- css - 如何使用具有多个渐变的顺风渐变实用程序?
- r - 时空分布动画。添加时间轴以获得更好的可视化
- ios - 在 Swift 中使用线程会导致内存泄漏
- python - TypeError:无法在 python 3 中腌制 _thread.RLock 对象
- javascript - JS:对象的嵌套数组搜索 - 部分列表格式
- sql - 用于选择记录以仅包含特定 ID 的本机查询
- java - 在生成战争之前,Gradle 将目录移动到根目录
- reactjs - 您如何在 Safari 上使用 NextJS 和 Firebase 身份验证“使用 Google 登录”
- oracle-jet - Oracle Jet,核心路由器 - 重新配置功能