1

hello i need help with array , as you can see my data

{
"age" : "18",
"altKategoriler" : [ "Dramalar" ],
"category" : [ "Aksiyon", "Heyecanlı", "Gerilim" ],
"id" : 5240718100,
"img" : "https://i.ibb.co/k8wx5C8/AAAABW9-ZJQOg-MRljz-Zwe30-JZw-Hf4vq-ERHq6-HMva5-ODHln-Ci-OEV6ir-Rcjt88tcnm-QGQCKpr-K9h-Oll-Ln-Sbb-EI.jpg",
"izlenilmeSayisi" : 0,
"logo" : "https://i.ibb.co/Rb2SrcB/AAAABfcrhh-Rni-Ok-Ct2l-Rys-ZYk-Oi-T0-XTeagkrw-Mkm-U0h-Lr-WIQZHEHg-VXihf-OWCwz-Vv-Qd7u-Ffn-DFZEX2-Ob.webp",
"oyuncuKadrosu" : [ "Diego Luna", "Michael Pena", "Scoot McNairy", "Tenoch Huerta", "Joaquin Cosio" ],
"senarist" : [ "Doug Miro" ],
"time" : "3 Sezon",
"title" : "Narcos: Mexico",
"type" : "Dizi",
"videoDescription" : "Guadalajara Karteli'nin yükselişinin gerçek öyküsünü anlatan bu yeni ve cesur Narcos hikâyesinde, Meksika'daki uyuşturucu savaşının 1980'lerdeki doğuşuna tanıklık edin.",
"videoQuality" : "HD",
"videosrc" : "https://tr.vid.web.acsta.net/uk/medias/nmedia/90/18/10/18/19/19550785_hd_013.mp4",
"year" : "2021",
"yonetmen" : [ "Carlo Bernard", "Chris Brancato" ]
}

I can access elements such as id , title or logo because they are not arrays.

How can I loop through the data inside the array since there is an array in the category in yield?

var data = this.database.filter((item) => item.type == searchType)

var data = this.database.filter((item) => item.category == searchCategory)

It's okay because my type value doesn't have an array.

But when I enter my category value, it only gets the first index[0]. It does not look at other indexes.

in summary, item.category[0] , item.category[1] , item.category[2]...........
How can I get index browsing like

  • 1
    You can have a look into: https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript – Rcdlb Feb 08 '22 at 07:26
  • 3
    To assert that a variable is in your array, use `Array.prototype.includes`, i.e.: `var data = this.database.filter((item) => item.category.includes(searchCategory))` – Terry Feb 08 '22 at 07:28
  • 1
    You can use includes() method to check in your Array in the Object contains a specific value. So your code would be: var data = this.database.filter((item) => item.category.includes(searchCategory)) – Ozgur Sar Feb 08 '22 at 07:30
  • includes worked exactly as I wanted thanks , how can I use it with match ? – Sinan Öztürk Feb 08 '22 at 09:31

2 Answers2

1

if your data looks like this :

let data ={
"age" : "18",
"altKategoriler" : [ "Dramalar" ],
"category" : [ "Aksiyon", "Heyecanlı", "Gerilim" ],
"id" : 5240718100,
"img" : "https://i.ibb.co/k8wx5C8/AAAABW9-ZJQOg-MRljz-Zwe30-JZw-Hf4vq-ERHq6-HMva5-ODHln-Ci-OEV6ir-Rcjt88tcnm-QGQCKpr-K9h-Oll-Ln-Sbb-EI.jpg",
"izlenilmeSayisi" : 0,
"logo" : "https://i.ibb.co/Rb2SrcB/AAAABfcrhh-Rni-Ok-Ct2l-Rys-ZYk-Oi-T0-XTeagkrw-Mkm-U0h-Lr-WIQZHEHg-VXihf-OWCwz-Vv-Qd7u-Ffn-DFZEX2-Ob.webp",
"oyuncuKadrosu" : [ "Diego Luna", "Michael Pena", "Scoot McNairy", "Tenoch Huerta", "Joaquin Cosio" ],
"senarist" : [ "Doug Miro" ],
"time" : "3 Sezon",
"title" : "Narcos: Mexico",
"type" : "Dizi",
"videoDescription" : "Guadalajara Karteli'nin yükselişinin gerçek öyküsünü anlatan bu yeni ve cesur Narcos hikâyesinde, Meksika'daki uyuşturucu savaşının 1980'lerdeki doğuşuna tanıklık edin.",
"videoQuality" : "HD",
"videosrc" : "https://tr.vid.web.acsta.net/uk/medias/nmedia/90/18/10/18/19/19550785_hd_013.mp4",
"year" : "2021",
"yonetmen" : [ "Carlo Bernard", "Chris Brancato" ]
}

and if we have array of data you can do something like this :

myArray.filter(item=>item.category.indexOf(searchCategory)>=0)

but if you want to explore in object rather than array you can do this :

data.category.indexOf(searchCategory)>=0
Sadaf Ziya
  • 37
  • 4
1

You could make this a bit generic, by testing whether the targeted field is an array, using Array.isArray, and then call a filter on each element, and see if any is positive (using .some()). The filter can be function that is provided, so that it can perform a simple match, or apply a regular expression, or anything else.

Instead of testing with Array.isArray you could skip that step and check whether the value has a .some() method. If so, calling it will give the desired outcome, and otherwise (using the .? and ?? operators), the filter should be applied to the value as a whole:

Here is how that looks:

function applyFilter(data, field, filter) {
    return data.filter(item => item[field]?.some(filter) ?? filter(item));
}

// Example use:
var data = [{
  "category" : [ "Action", "Thriller", "Horror"],
  "type" : "Series",
}, {
  "category" : [ "Historical", "Romance" ],
  "type" : "Theatre",
}];

// Find entries that have a category that looks like "roman*":
var result = applyFilter(data, "category", value => /^roman.*/i.test(value));

console.log(result);

If you are running on an older version of JavaScript, and don't have support for .? or ??, then use:

return data.filter(item => Array.isArray(item[field])
                         ? item[field].some(filter)
                         : filter(item));
trincot
  • 317,000
  • 35
  • 244
  • 286
  • Dots and question marks give an error in vue js, thank you very much for providing detailed information, but I could not run it. – Sinan Öztürk Feb 09 '22 at 00:54
  • Seems like you are running on an older version of ECMAScript then. See addition to answer for using syntax supported by older versions. – trincot Feb 09 '22 at 06:48