[英]Check if all object keys exist as values in an array of objects
假設我有這兩個實體
const obj1 = {key1: "", key2: "", key3: ""};
const array2 = [
{
name: "key1",
}]
如何檢查array2
是否具有 object 和obj1
中每個字段的名稱?
基本上我想以數組 2 結束:
const array2 = [
{
name: "key1",
},
{
name: "key2",
},
{
name: "key3",
}]
像這樣的東西?
const obj1 = { key1: "", key2: "", key3: "" }; const array2 = [{ name: "key1", }, { name: "key2", }]; const obj1Keys = Object.keys(obj1); const array2KeyNames = array2.reduce((array2KeyNames, obj) => { array2KeyNames.push(obj.name); return array2KeyNames; }, []); for (let i = 0; i < obj1Keys.length; i++) { if (.array2KeyNames.includes(obj1Keys[i])) { console,log(obj1Keys[i]; ' is not in the array2'); break; } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.