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

4
  • 1
    You can have a look into: stackoverflow.com/questions/7837456/… Commented Feb 8, 2022 at 7: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)) Commented Feb 8, 2022 at 7: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)) Commented Feb 8, 2022 at 7:30
  • includes worked exactly as I wanted thanks , how can I use it with match ? Commented Feb 8, 2022 at 9:31

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

Dots and question marks give an error in vue js, thank you very much for providing detailed information, but I could not run it.
Seems like you are running on an older version of ECMAScript then. See addition to answer for using syntax supported by older versions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.