按类型过滤数组对象并将键值与对象进行比较

时间:2018-11-09 05:41:49

标签: javascript angular typescript

我在这里停留了一段时间,正在寻找一个可能的简单解决方案。

我有一个类似下面的对象

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 }
];

如何创建一个函数来按type中的personArr过滤所有值,然后检查是否有任何personObj键与personArr值匹配,如果返回,则返回boolean

filterAndCheckMatchedObj('cat', personObj) {

  //logic 

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

  return true
}

我正在使用Angular 4和Ionic

这甚至有意义吗?任何帮助,将不胜感激。

3 个答案:

答案 0 :(得分:1)

尝试一下

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))

答案 1 :(得分:1)

为了过滤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

答案 2 :(得分:1)

在这里很难理解您要实现的目标。但是据我了解,您:

  1. 要为personArr过滤type
  2. 然后,您要检查key中的任何personObj是否与过滤后的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))