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

201
Views
using flatmap to separate array with comma

I have the following array:

[
  ["Polymeric", "Vehicle Graphics (Basic)"],
  ["Cast", "Vehicle Graphics (Part Wrap), Vehicle Graphics (Full Wrap)"],
  ["Polymeric", "Vehicle Graphics (Part Wrap)"]
]

I need this:

[
  ["Polymeric", "Vehicle Graphics (Basic)"],
  ["Cast", "Vehicle Graphics (Part Wrap)"],
  ["Cast", "Vehicle Graphics (Full Wrap)"],
  ["Polymeric", "Vehicle Graphics (Part Wrap)"]
]

Here's my code:

const source = [ 
  ["Polymeric", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "Vehicle Graphics (Basic)"],
  ["Cast", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "Vehicle Graphics (Part Wrap), Vehicle Graphics (Full Wrap)"],
  ["Polymeric", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "Vehicle Graphics (Part Wrap)"]
];


source.flatMap(([key, value1, value2, value3, value4, value5, value6, value7, value8, value9, value10, value11, value12, value13, value14, value15, value16]) => { 

  const values = value16.split(', ') 

  var hjk = values.map(singleValue => [key, singleValue])
  console.log(hjk);
})

Here's a jsfiddle of it working.

Is there a better way to do this? Putting value1, value2 etc seems silly.

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

0

You could destructure the wanted values and map the wanted pairs.

const
    source = [["Polymeric", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "Vehicle Graphics (Basic)"], ["Cast", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "Vehicle Graphics (Part Wrap), Vehicle Graphics (Full Wrap)"], ["Polymeric", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "Vehicle Graphics (Part Wrap)"]],
    result = source.flatMap(({ 0: key, 16: values }) => values
        .split(', ')
        .map(value => [key, value])
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

You could do this:

const source = [ 
  ["Polymeric", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "Vehicle Graphics (Basic)"],
  ["Cast", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "Vehicle Graphics (Part Wrap), Vehicle Graphics (Full Wrap)"],
  ["Polymeric", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "Vehicle Graphics (Part Wrap)"]
];


source.flatMap((arr) => { 
  let [key] = arr;
  let value16 = arr[16];
  const values = value16.split(', ') 

  var hjk = values.map(singleValue => [key, singleValue])
  console.log(hjk);
})

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!