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

144
Views
JavaScript array comparisions and display values from another

I have a case where I need to compare between two arrays and display values from another.; For example, I have two arrays:

let a = ['a','b','c'];
let b = ['textA' 'textB', ' '];

So, I am basically trying to loop over the array b and display value like this:

   textA
   textB
   C

So, when there are any empty values found in array b, display the same indexed values from array a.

Can anybody help with this. Thanks in advance.

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

0

you can :

  • trim the value to see if there is empty or only space elem.trim().length
  • if string is empty check if data in other array exist if (!elem.trim().length && a[index])

let a = ['a','b','c'];
let b = ['textA', 'textB', ' '];

b.forEach((elem, index) => {
  if (!elem.trim().length && a[index]) {
    console.log(a[index]);
  } else {
    console.log(elem);
  }
});

One other solution consist to create a result array with array.map and display all key of this new array

let a = ['a','b','c'];
let b = ['textA', 'textB', ' '];


let result = b.map((elem, index) => (!elem.trim().length && a[index]) ? a[index] : elem);

console.log(result);

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!