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

106
Views
Mapping 2D string array to Int

I want to convert the first element to an int like the following

[["bad", ddd, 2.0, 1.0], ["great",ddd , 3.0, 1.0], ["fair",ddd , 4.0, 1.0], ["great",ddd , 5.0, 1.0]]

↓

[[0, ddd, 2.0, 1.0], [2,ddd , 3.0, 1.0], [1,ddd , 4.0, 1.0], [2,ddd , 5.0, 1.0]]

what is the best way to do this? I have tried to use map but have no success.

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

0

const map = new Map();
map.set("bad", 0);
map.set("great", 2);
map.set("fair", 1);

const param = [["bad", 'zzz', 2.0, 1.0], ["great",'ddd' , 3.0, 1.0], ["fair",'ddd' , 4.0, 1.0], ["great",'ddd6' , 5.0, 1.0]];

param.map(([a,b,c,d]) => {
    return [map.get(a), b, c, d];
});
about 4 years ago · Juan Pablo Isaza Report

0

ddd is string, so "ddd". ratings is a filtering array, when a match is found with .indexOf() it returns an index number:

sub[0] = ratings.indexOf(sub[0]);

// Utility function
const log = data => console.log(JSON.stringify(data));

const data = [
  ["bad", 'ddd', 2.0, 1.0],
  ["great", 'ddd', 3.0, 1.0],
  ["fair", 'ddd', 4.0, 1.0],
  ["great", 'ddd', 5.0, 1.0]
];
const ratings = ["bad", "fair", "great"];

const result = data.map(sub => {
  sub[0] = ratings.indexOf(sub[0]);
  return sub;
});

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!