如何检查 JavaScript 对象中是否存在特定键。

huangapple go评论124阅读模式
英文:

How to check whether there is some specific keys or not in object in js

问题

我想通过一些逻辑验证'key'来获得这个结果。

const validateOfKey= [ 'a', 'b' ] 

const pass1 = { a: 3, b: 4 } // true
const pass2 = { a: 3 } // true

const pass3 = { a:3, c:5, .. } // false
const pass4 = { a:3, b:4, c:3...} // false
const pass5 = { d:3, e:5 ...} // false

在某些情况下,我可以使用hasOwnProperty。但在我的情况下,对我来说有点难以制定这个逻辑。

您能推荐一些建议来解决这个问题吗?非常感谢您的阅读。

英文:

I want to get this result by some logic through a validate for 'key'.<br>

const validateOfKey= [ &#39;a&#39;, &#39;b&#39; ] 

const pass1 = { a: 3, b: 4 } // true
const pass2 = { a: 3 } // true

const pass3 = { a:3, c:5, .. } // false
const pass4 = { a:3, b:4, c:3...} // false
const pass5 = { d:3, e:5 ...} // false

I can use hasOwnProperty for some case. But in my case, this is a little bit hard for me to make this logic.<br>
Can you recommend some advice for resolving this ? Thank you so much for reading it.

答案1

得分: 2

使用Object.keys()来获取键的数组,并检查每个键是否包含在有效键的列表中:

const fn = (validKeys, obj) =>
  Object.keys(obj)
    .every(k => validKeys.includes(k))

const validateOfKey= ['a', 'b'] 

console.log(fn(validateOfKey, { a: 3, b: 4 })) // true
console.log(fn(validateOfKey, { a: 3 })) // true

console.log(fn(validateOfKey, { a:3, c:5 })) // false
console.log(fn(validateOfKey, { d:3, e:5 })) // false
英文:

Use Object.keys() to get an array of keys, and check if every key is included in the list of valid keys:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const fn = (validKeys, obj) =&gt;
  Object.keys(obj)
    .every(k =&gt; validKeys.includes(k))

const validateOfKey= [&#39;a&#39;, &#39;b&#39;] 

console.log(fn(validateOfKey, { a: 3, b: 4 })) // true
console.log(fn(validateOfKey, { a: 3 })) // true

console.log(fn(validateOfKey, { a:3, c:5 })) // false
console.log(fn(validateOfKey, { d:3, e:5 })) // false

<!-- end snippet -->

答案2

得分: 2

你可以使用Set并根据对象的键进行检查。

function has(object, keys) {
    return Object.keys(object).every(Set.prototype.has, new Set(keys));
}

const keys = ['a', 'b'];

console.log(has({ a: 3, b: 4 }, keys));       //  true - 所有键都存在
console.log(has({ a: 3 }, keys));             //  true - 键的子集存在
console.log(has({ a: 3, c: 5 }, keys));       // false - 存在其他键
console.log(has({ a: 3, b: 4, x: 3 }, keys)); // false - 存在其他键
console.log(has({ d: 3, e: 5 }, keys));       // false - 没有所需的键
英文:

You could take a Set and check against the keys from the object.

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function has(object, keys) {
return Object.keys(object).every(Set.prototype.has, new Set(keys));
}

const keys = [&#39;a&#39;, &#39;b&#39;];

console.log(has({ a: 3, b: 4 }, keys));       //  true - all keys
console.log(has({ a: 3 }, keys));             //  true - subset of keys
console.log(has({ a: 3, c: 5 }, keys));       // false - some other key/s
console.log(has({ a: 3, b: 4, x: 3 }, keys)); // false - some other key/s
console.log(has({ d: 3, e: 5 }, keys));       // false - no wanted keys

<!-- end snippet -->

答案3

得分: 1

迭代遍历 validateOfKey 数组中的所有键,并检查给定对象是否包含该键。为此,我们使用 "key in Object"。

var validate = inputObj => {
    let validateOfKey = ["a", "b"];

    for (let i = 0; i < validateOfKey.length; i++) {
        if (!(validateOfKey[i] in inputObj)) {
            return false;
        }
    }
    return true;
};

将任何对象传递给上述函数以检查是否包含 validateOfKey 中的键。您还可以根据需要修改 validateOfKey。

英文:

iterate through all keys in validateOfKey array and check if the given object contains that key. for this we use "key in Object"

var validate = inputObj =&gt; {
    let validateOfKey = [&quot;a&quot;, &quot;b&quot;];

    for (let i = 0; i &lt; validateOfKey.length; i++) {
      if (!(validateOfKey[i] in inputObj)) {
        return false;
      }
      return true;
    }
  };

pass any object to above function to check against keys in validateOfKey.
also you can modify validateOfKey as per your needs.

huangapple
  • 本文由 发表于 2020年1月3日 18:10:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定