Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
Filter an array of object, if X cannot find looking for Y

I have an array of object and I want to make a filter when a language does not exist use an another one. For example when I'm searching for the langauge de and it is not exist I want to search in this case to language en

const arrObj = [
  {id: 0, name: 'Zero', lang: 'hu', video: 'Clip'},
  {id: 1, name: 'One', lang: 'en', video: 'Clip'},
  {id: 2, name: 'Two', lang: 'en', video: 'Clip'},
  {id: 3, name: 'Three', lang: 'en', video: 'Trailer'},
  {id: 4, name: 'Four', lang: 'hu', video: 'Clip'},
  {id: 5, name: 'Five', lang: 'en', video: 'Trailer'},
  {id: 6, name: 'Six', lang: 'hu', video: 'Trailer'},
  {id: 7, name: 'Seven', lang: 'hu', video: 'Clip'},
]
const language = 'de'
const item = arrObj.filter(e => {
  if (e.lang === language && e.video === 'Trailer') {    
    return e.video === 'Trailer'
  } 
})

I've tried with find function and with different if else statement as well. I'm really stuck with this now. Any help would be appreciated

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This will search using all given keywords until one of them returns some result. If no result is found, returns empty array;

let searchTerms = ["de", "be", "en"]

const arrObj = [{
        id: 0,
        name: 'Zero',
        lang: 'hu',
        video: 'Clip'
    },
    {
        id: 1,
        name: 'One',
        lang: 'en',
        video: 'Clip'
    },
    {
        id: 2,
        name: 'Two',
        lang: 'en',
        video: 'Clip'
    },
    {
        id: 3,
        name: 'Three',
        lang: 'en',
        video: 'Trailer'
    },
    {
        id: 4,
        name: 'Four',
        lang: 'hu',
        video: 'Clip'
    },
    {
        id: 5,
        name: 'Five',
        lang: 'en',
        video: 'Trailer'
    },
    {
        id: 6,
        name: 'Six',
        lang: 'hu',
        video: 'Trailer'
    },
    {
        id: 7,
        name: 'Seven',
        lang: 'hu',
        video: 'Clip'
    },
];


let search = (data, keywords) => {
    let filteredData = [];
    for (let j = 0; j < keywords.length; j++) {

        filteredData = data.filter(x => x.lang === keywords[j])
        if (filteredData.length != 0) {
            return filteredData;
        }

    }
    return filteredData;


}

console.log(search(arrObj, searchTerms))

about 4 years ago · Juan Pablo Isaza Report

0

You can try using the logical or operator. Right side of the operator will only be evaluated when there is no trailer in de since find returns undefined (which is falsy) when there's no match:

const arrObj = [
  {id: 0, name: 'Zero', lang: 'hu', video: 'Clip'},
  {id: 1, name: 'One', lang: 'en', video: 'Clip'},
  {id: 2, name: 'Two', lang: 'en', video: 'Clip'},
  {id: 3, name: 'Three', lang: 'en', video: 'Trailer'},
  {id: 4, name: 'Four', lang: 'hu', video: 'Clip'},
  {id: 5, name: 'Five', lang: 'en', video: 'Trailer'},
  {id: 6, name: 'Six', lang: 'hu', video: 'Trailer'},
  {id: 7, name: 'Seven', lang: 'hu', video: 'Clip'},
]
const language = 'de';


const item = arrObj.find(e => e.video == 'Trailer' && e.lang === language)
                || arrObj.find(e => e.video == 'Trailer' && e.lang ===  'en');

console.log(item);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!