Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

boyjarv's avatar

check if object is in array then show

Hi,

I have a list of courses, all have EN as default language but some will have DE and FR and some may have ES aswell Do I write an array and add in all the 2 char languages and then say if in languages array then display the object?

I can't check against each one? if(fr) { show FR object here } if(es) { show ES object here }

courses = [
{ 
id: 1,
en: object,
de: object,
},
{ 
id: 2,
en: object,
fr: object,
},
{ 
id: 3,
en: object,
de: object,
fr: object,
}
{ 
id: 4,
en: object,
de: object,
fr: object,
es: object,
}
]

or can I just create an array: ['EN', 'FR', 'DE', 'ES']

and then say IF obj is IN Array THEN show the object?

0 likes
8 replies
boyjarv's avatar

ok so I added an array of language codes to my data:

data: () => ({
    courses: [],
    languagesList: [
      { code: "ab", name: "Abkhazian" },
      { code: "aa", name: "Afar" },
      { code: "af", name: "Afrikaans" },
      { code: "ak", name: "Akan" },
...

and then added another 2 v-for loops

<div v-for="course in courses" :key="course.id">
          <ul v-for="coursex in languagesList.code" :key="coursex">
            {{
              coursex
            }}
            <li v-for="n in languages(coursex)" :key="n.id">{{ n }}x</li>
          </ul>
          <span>
...

and my languages method to hopefully spit out the languages that are in my array?!

languages: function (lang) {
      return lang.includes(function (lang) {
        return lang;
      });
    },

still not working but I think I kind of have the right concept?!

MaverickChan's avatar

use computed property as a boolean , then show data based on if

martinbean's avatar

@boyjarv It’d be easier if you explained what you’re expecting to see? If you’re looping over all languages just to display each one, why are you then checking if a language exists? What are you actually trying to output here?

boyjarv's avatar

ok so I can guess if DE exists and it does:

async fetchCourses() {
      const courseResponse = await axios.get(
        `http://127.0.0.1:8000/api/course/${this.$route.params.course}`
      );
      this.coursesuite = courseResponse.data.de.config.coursesuite;
      this.coursename = courseResponse.data.de.config.coursename;
      this.courseid = courseResponse.data.de.config.courseid;
      this.lms = courseResponse.data.de.config.lms;
      this.defaultlang = courseResponse.data.de.config.defaultlang;
      console.log("HERE I AM: ", courseResponse.data.de.config.coursesuite);
    },

but what if ES or FR don't exist?!

boyjarv's avatar

How can I determine if I course has more than 1 language?!

martinbean's avatar

@boyjarv By putting all available languages in an array, and then counting the array!

[
    {
        id: 1,
        availableLanguages: [
            {
                lang: "en",
                description: "Course description in English."
            },
            {
                lang: "es",
                description: "Course description in Spanish."
            },
        ],
    },
]
for (course in courses) {
    if (course.availableLanguages.length > 1) {
        // Course is available in multiple languages
    }
}
boyjarv's avatar

I suppose I could wrap them in an array

Please or to participate in this conversation.