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

171
Views
Convert nodejs exec output to array

How can I convert the shell command output to an array so I can print variables as uniqueMetrics[0], uniqueMetrics[1] etc?

const uniqueMetrics = await execShellCommand('aws cloudwatch list-metrics --namespace AWS/ApiGateway  | jq -r .Metrics[].Dimensions[].Name | sort -u')

console.log(uniqueMetrics);

output:

ApiId
ApiName
Stage

It seems the data is returned as follows with \r and \n.

[ 'ApiId\r\nApiName\r\nStage\r\n' ]

So I'm guessing I'd need to search and replace these strings first. Am I thinking right?

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

0

You should be able to use String.split() function:

let uniqueMetricsArray = uniqueMetrics.split('\r\n');

For better compatibility, it might make sense to make it work for both \r\n and just \n using RegExp:

let uniqueMetricsArray = uniqueMetrics.split(/\r?\n/);
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!