简体   繁体   English

获取具有属性的数组中的对象,该属性是包含匹配值的数组

[英]Get an Object in an Array that has a property which is an Array that contains a matching value

I have an Array of Objects with a structure like this: 我有一个具有这样的结构的对象数组:

[
  {
    title: "", 
    imagePoster: "", 
    episodes: "", 
    score: "",
    genres: ["Comedy", "Drama", ...],
    description: ""
  },
  ...
]

I need to return an Object which contains specific value like "Comedy" in the genres Array. 我需要返回一个genres数组中包含特定值(例如"Comedy"的对象。 I've been trying a lot of ways like for , filter , map , indexOf ... But, I still can't figure it out, unless I search for ["Comedy", "Drama", ...] (ie, the whole Array). 我一直在尝试很多方法,例如forfiltermapindexOf ...。但是,除非我搜索["Comedy", "Drama", ...] (即,整个数组)。

You could use Array#find to find the first element that satisfies the condition: 您可以使用Array#find查找满足条件的第一个元素:

const search = "Comedy";
const found = array.find(obj => obj.genres.includes(search));

This also uses Array#includes which checks if the argument, the search keyword, exists in the genres array of each object. 它还使用Array#includes来检查参数(搜索关键字)是否存在于每个对象的genres数组中。 And if you needed IE support, use Array#indexOf and check if the return value is greater than -1 instead which does exactly the same thing: 并且如果需要IE支持,请使用Array#indexOf并检查返回值是否大于-1,而返回值是否完全相同:

obj => obj.genres.indexOf(search) > -1

And if you wanted to return all objects that have a certain genre, not just the first one, filter the array with Array#filter which filters the array based on a condition: 并且,如果您想返回所有具有特定类型的对象,而不仅仅是第一个,请使用Array#filter过滤该数组,后者根据条件过滤该数组:

const search = "Comedy";
const found = array.filter(obj => obj.genres.includes(search));

This will filter the array and only leave the objects that include the search keyword in their genres array. 这将过滤数组,仅将包含搜索关键字的对象保留在其genres数组中。 Again, you can swap out includes for the indexOf check, and you can always just access the first element of the filtered array if find is not supported on your target browsers (namely IE). 同样,您可以将includes换为indexOf检查,并且如果目标浏览器(即IE)不支持find则始终可以仅访问过滤后的数组的第一个元素。

Array.find & Array.includes are your friends. Array.findArray.includes是您的朋友。 There are also polyfills for non-browsers :) 也有非浏览器的polyfills :)

The most supported way would be to use Array.filter & Array.indexOf 最受支持的方法是使用Array.filterArray.indexOf

function getMovieByGenre(genre) {
  return movies.filter(function(movie) {
    return movies.genres.indexOf(genre) >= 0
  })[0];
}

var someComedy = getMovieByGenre('Comedy');
if (someComedy) {
  // movie found
}

I am using the most supported code, without polyfills and ES6 features 我使用的是最受支持的代码,没有polyfills和ES6功能

暂无
暂无

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

相关问题 使用javascript获取属性包含数组的对象作为值 - get the object which property contains an array as the value using javascript 获取数组中包含字符串中最匹配ID属性的对象? - Get object in array that contains closest matching id property in a string? 如何检查数组是否包含值,这是对象的属性? - How to check if array contains value, which is a property of an an object? 从包含特定值的数组中获取对象 - Get an object from array which contains a specific value 如何获取包含在嵌套数组中给定值的对象中的项目 - How get items in object which contains in nested array given value JavaScript获得对象数组的值或属性(如果对象具有该属性) - JavaScript get value or property of an Array of objects if object has that property 检查数组是否包含具有特定属性值的对象 - Check if array contains an object with the value of a specific property 如何检查包含另一个对象数组的对象数组是否具有属性 - How to check if array of objects that contains another array of object has property 如果 Object 在另一个数组中有匹配值,则反应 Object 的更新数组 - React Update Array of Object if Object has a matching value in another Array 得到一个包含 2 个对象的数组。每个对象都有一个属性 `events`,其中包含一个对象数组。 如何过滤参与者ID不为1的元素? - Got an array of 2 objects.Each object has a property `events` which contains an array of objects. How to filter elements whose participantId is not 1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM