簡體   English   中英

按類型過濾數組對象並將鍵值與對象進行比較

[英]Filter Array Object by type and compare key value against Object

我在這里停留了一段時間,正在尋找一個可能的簡單解決方案。

我有一個像下面的對象

personObj  = { catname: "somecat" ,catname2: "somecat", dogname: "ruff", horsename: "sam"};

然后像arrayObject

personArr = [
  { key: "a1", value: "catname" type: "cat" },
  { key: "a2", value: "catname2", type: "cat" },
  { key: "a3", value: "somedog" type: "dog" },
  { key: "a4", value: "horsename" type : "horse }
];

我如何創建一個函數來按personArrtype過濾所有值,然后檢查是否有任何personObj鍵與personArr值匹配,如果返回,則返回boolean

filterAndCheckMatchedObj('cat', personObj) {

  //logic 

  // Filtered by cat  2 matches (catname and catname2) 

  return true
}

我正在使用Angular 4和Ionic

這有道理嗎? 任何幫助,將不勝感激。

嘗試這個,

 var personObj = { catname: "somecat" ,catname2: "somecat", dogname: "ruff", horsename: "sam"} var personArr = [{"key":"a1","value":"catname","type":"cat"},{"key":"a2","value":"catname2","type":"cat"},{"key":"a3","value":"somedog","type":"dog"},{"key":"a4","value":"horsename","type":"horse"}]; function find(type, personObj){ return personArr.filter(item=>item.type === type).filter(item=>(item.value in personObj)).length !== 0 } console.log(find('cat', personObj)) 

為了過濾parsonArr,您需要將其作為一個數組:

personArr  = [
    { key: "a1", value: "catname", type: "cat" },
    { key: "a2", value: "catname2", type: "cat" },
    { key: "a3", value: "somedog", type: "dog" },
    { key: "a4", value: "horsename", type : "horse" }
];

請注意,每個鍵:值都需要用逗號分隔(有時會漏掉)。 另外,您忘了關閉"horse"

在函數聲明中,您無需傳遞參數,只需聲明它們即可:

filterAndCheckMatchedObj(animalType, personObj) {
    const filteredParsonArr = personArr.filter(person => person.type === animalType);
}

現在,您需要personArr與personObj之間的任何關系。

var personObj = { catname: "somecat",
                   catname2: "somecat",
                   dogname: "ruff",
                   horsename: "sam"
                 };

最后一件事:

我建議您更改變量名稱。 您沒有personArr並且他的對象描述了動物是沒有道理的。

總結一下:

 const personArr = [ { key: "a1", value: "catname", type: "cat" }, { key: "a2", value: "catname2", type: "cat" }, { key: "a3", value: "somedog", type: "dog" }, { key: "a4", value: "horsename", type : "horse" } ]; const personObj = { catname: "somecat" ,catname2: "somecat", dogname: "ruff", horsename: "sam"} function filterAndCheckMatchedObj(animalType, personObj) { return personArr.some(animal => animal.type === animalType && (animal.value in personObj) ); } alert(filterAndCheckMatchedObj('cat', personObj)); // true alert(filterAndCheckMatchedObj('fish', personObj)); // false 

在這里很難理解您要實現的目標。 但是據我了解,您:

  1. 要過濾typepersonArr
  2. 然后,你要檢查是否有任何的keypersonObj匹配任何的values的關鍵value的過濾personArr
  3. 在這種情況下,返回true 否則返回false

因此,嘗試一下:

 var personObj = { catname: "somecat", catname2: "somecat", dogname: "ruff", horsename: "sam" }; var personArr = [ { "key": "a1", "value": "catname", "type": "cat" }, { "key": "a2", "value": "catname2", "type": "cat" }, { "key": "a3", "value": "somedog", "type": "dog" }, { "key": "a4", "value": "horsename", "type": "horse" } ]; function find(type, personObj) { var filteredArray = personArr.filter(item => item.type === type); var objectKeys = Object.keys(personObj); var arrayValues = filteredArray.map(item => item.value); return objectKeys.filter(element => arrayValues.includes(element)).length > 0; } console.log(find('cat', personObj)) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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