【问题标题】:Filter if value inside array exists has key in object [duplicate]过滤数组中的值是否存在对象中的键[重复]
【发布时间】:2022-10-12 22:01:32
【问题描述】:
array = ['data', 'category', 'hour'];

    object = {
        "status": {
            "type": "INFO",
            "messages": []
        },
        "data": {
            "id": 1,
            "tenant": "675832",
            "process": "6911d872-35f8-11ea-8697-001dd8b71c20",
            "category": "resquests"
"time": {
hour: "12",
minute: "30"
        }
    }

我需要检查对象是否具有数组中包含的相同值的键。

我尝试按点拆分数组,然后过滤数组和对象,但失败了。

const array = inputValue.split('.').map((item) => item);

【问题讨论】:

  • const value = array.reduce((acc, p) => acc?.[p], object)
  • Object.keys(object).filter(item => array.includes(item))

标签: javascript arrays object filter


【解决方案1】:

你需要这样的东西吗?

console.log(arrayEquals(Object.keys(object),array)

function arrayEquals(a, b) {
  return Array.isArray(a) &&
    Array.isArray(b) &&
    a.length === b.length &&
    a.every((val, index) => val === b[index]);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 2021-11-17
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    相关资源
    最近更新 更多