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

162
Views
Cut the first word out of an array of strings and store in a new variable

I am currently getting back an array like this:

['Alica Brereton,Marijuana,9.18,50',
  'William Kotai,ecstasy,19.12,20',
  'Joel Forro,heroin,91.16,5',
  'David Ernest,Methamphetamine,108.78,5',
  'David Ernest,cocaine,80,2',
  'Joel Forro,ecstasy,19.12,10',
  'Gabriella Hyde,Marijuana,9.18,10',
  'Gabriella Hyde,Methamphetamine,108.78,8',
  'Marijuana,9.18,10'
]

I want to cut the names at the start of each string and store in a new variable, so I would have something like this:

['Alica Brereton',
  'William Kotai',
  'Joel Forro',
  'David Ernest',
  'Joel Forro',
  'Gabriella Hyde'
]

i'm working of a text file here is my current

code: 
const fs = require("fs");
const data = fs.readFileSync("data.txt").toString();
const lines = data.split("\n");

let customerKeys = lines[0].replace("customer", "Total Price");
lines.shift();
console.log(customerKeys);
console.log(lines);

let array = lines.filter((str) => {
  return str !== "";
});
let arrNoSpaces = array;

console.log(arrNoSpaces);
arrNoSpaces[6].replace("Gabriella Hyde,", "");
var GabriellaExtra = arrNoSpaces[6].replace("Gabriella Hyde,", "");
console.log(GabriellaExtra);

arrNoSpaces.push(GabriellaExtra);
console.log(arrNoSpaces);
arrNoSpaces.splice(-3, 1);

console.log(arrNoSpaces);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

A regular expression can match letters and spaces (not commas) from each string, mapping to a new array.

const arr = ['Alica Brereton,Marijuana,9.18,50',
  'William Kotai,ecstasy,19.12,20',
  'Joel Forro,heroin,91.16,5',
  'David Ernest,Methamphetamine,108.78,5',
  'David Ernest,cocaine,80,2',
  'Joel Forro,ecstasy,19.12,10',
  'Gabriella Hyde,Marijuana,9.18,10',
  'Gabriella Hyde,Methamphetamine,108.78,8',
  'Marijuana,9.18,10'
];

const result = arr
  .map(line => line.match(/\w+ \w+/)?.[0]) // extract matches
  .filter((item, i, arr) => item && arr[i - 1] !== item) // filter out failed matches and dupes
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!