首页 > 解决方案 > 基于真标志比较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"}
];

你看没有约翰了,因为它是假的。我什至不知道如何比较这两个值。

标签: javascript

解决方案


您可以使用filterArray 对象中的函数:

const filteredValues = values.filter(({ firstValue }) => incomingValues[firstValue]);

推荐阅读