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

207
Views
Javascript - How to convert numbers to String in an array and use If Statement

Ex: arr = [1,1,2,3];

If the values is 1 , it should change to red

If the values is 2 , it should change to blue

If the values is 3 , it should change to white

Expected output:

arr = [red,red,blue,white]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

you can map your Array :

var nn = arr.map(e=> e == 1 ? e = "red" : e == 2 ? e = "blue" : e = "white")
about 4 years ago · Juan Pablo Isaza Report

0

Like so you are also able to filter out values that can't be mapped like 4 in this example.

[1,1,2,3,4].map(number => {
  switch(number) {
      case 1: 
        return 'red'
      case 2: 
        return 'blue'
      case 3:
        return 'white'
      default:
        return undefined 
  }
}).filter(name => name)
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!