簡體   English   中英

檢查所有 object 鍵是否作為對象數組中的值存在

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM