简体   繁体   English

检查对象数组中的任何键是否包含来自数组数组对象的值的最佳方法是什么?

[英]What would be the best way to check if any of the keys in an array of objects contain a value from an object of arrays array?

I want to create a filter system by checking if any of the project technologies exist in the arrOfObjs.name 's.我想通过检查arrOfObjs.name存在任何项目技术来创建过滤系统。 If it does, then it will pass the filter and be included/shown to the DOM.如果是,那么它将通过过滤器并被包含/显示到 DOM。 More specifically a computed value in VueJS.更具体地说,是 VueJS 中的计算值。

Example:例子:

arrOfObjs = [{name: 'test1', image: 'testing1'}, {name: 'test2', image:'testing2'}]
projects: 
[
      {
        name: "testproject",
        description: "lorem ipsum",
        technologies: ["test2", "test7", "test3"]
      },
      {
        name: "atest",
        description: "lorem ipsum",
        technologies: ["test1", "test2", "test5"]
      },
]

My attempt:我的尝试:

computed: {
  myComputedVal () {
    projs = []
    this.projects.forEach(p => {
      p.technologies.forEach(t => {
        this.arrOfObjs.filter(o => {
          if (o.name == t) {
            return p // maybe projs = [...projs, p] and return projs
          }
        })
      })
    })
  }
}

I want to check if ANY of the values in technologies exist in arrOfObjs.name , if it does, then return the project or push it to an array to later return this array of objects as a computed value.我想检查arrOfObjs.name中是否存在技术中的任何值,如果存在,则返回项目或将其推送到一个数组,以便稍后将此对象数组作为计算值返回。 At the moment, nothing happens.目前,什么都没有发生。

I think with the below code snippet you can filter out those projects where any of the technologies are present in the arrOfObjs array's name properties.我认为使用下面的代码片段,您可以过滤掉那些在arrOfObjs数组的name属性中存在任何technologies项目。

Using flatMap() you can get all the names into an array as strings first.使用flatMap()您可以首先将所有名称作为字符串放入数组中。 Then using filter() as you wanted originally and with using some() and includes() combination just filter out the projects if any of the technologies elements are represented in names array like:然后按照您最初的需要使用filter()并使用some()includes()组合,如果任何技术元素在names数组中表示,则只需过滤掉项目,例如:

({technologies}) => technologies.some(t => names.includes(t))

If you would like to check if all the elements are present, then suggest to use every() like:如果您想检查所有元素是否都存在,那么建议使用every()例如:

({technologies}) => technologies.every(t => names.includes(t))

Possible solution - representing the example with some() :可能的解决方案 - 用some()表示示例:

 const arrOfObjs = [{name: 'test1', image: 'testing1'}, {name: 'test2', image:'testing2'}], projects = [{name: "testproject",description: "lorem ipsum",technologies: ["test2", "test7", "test3"]}, {name: "atest",description: "Lorem ipsum", technologies: ["test1", "test2", "test5"] }]; const names = arrOfObjs.flatMap(e => e.name); const result = projects.filter( ({technologies}) => technologies.some(t => names.includes(t) // technologies.every(t => names.includes(t) // if all needed ) ); console.log(result);

I hope that helps!我希望这有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查还包含嵌套的 arrays 和对象的对象数组是否相同,如果是,则将它们合并为一个 object 并将键添加到一起 - Check if array of objects, which also contain nested arrays with objects, are the same, if so, combine them into one object and add the keys together 从对象数组中查找对象索引的最佳方法是什么 - javascript - What is the best way to find index of object from array of objects - javascript 在Javascript中检查对象是否为数组的最佳方法是什么? - What is the best way to check if an object is an array or not in Javascript? 在具有不同值(数组)的对象数组中查找对象的最有效方法是什么? - What would be the most efficient way to find an object in an array of objects with distinct value which is an array? 将对象数组转换为具有分组键作为数组的对象的简单方法 - Simple way to turn array of objects into object with grouped keys as arrays 从数组中删除对象的最佳方法是什么 - What is the best way to delete objects from Array 将 object 与 arrays 转换为对象数组的最佳方法,反之亦然 - best way to convert object with arrays to array with objects and viceversa 从数组中的键向对象数组 object 添加键和值 - Add key and value to array of objects object from the keys in array 有没有一种最佳方法可以根据其中一个键的值从 object 数组中返回所有值。 使用 array.reduce 或 array.forEach - Is there a best way to return all values from an array of object based on the value of one of the keys. Using array.reduce or array.forEach 如何检查对象数组是否包含数组中的任何值 - How to check if array of objects contains any value from array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM