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

202
Views
Get concatened string from variable in javascript object with map function

I have this object below:

var products = [
        {
           "id":"xxx",
           "name":"AAA"
        },
        {
           "id":"xxx",
           "name":"BBB"
        }
     ];

I´m trying to mapping the object and generate a concatenated string from all the values ´name´ using the map function and then join to concatenate the comma:

var result = products.map(p => Object.values(p.name).join(','));

But I'm getting this output with console.log:

"A,A,A, ,B,B,B"

The is one more comma there and plus it takes each character instead of the full key name value. Am I not accessing the array object in the right way?

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

0

Currently you're turning a string into an array and joining each character with a comma between them. You need to move the join outside of the map iterator function. There is also no need for the Object.value() call.

const result = products
  .map((product) => product.name)
  .join(', ')

var products = [
  {
     "id":"xxx",
     "name":"AAA"
  },
  {
     "id":"xxx",
     "name":"BBB"
  }
];

const result = products.map((product) => product.name).join(', ')

document.getElementById('root').innerHTML = result
<div id="root"></div>

about 4 years ago · Juan Pablo Isaza Report

0

Use Array.map and Array.join

Logic

  • Create a list of names from products using Array.map.
  • From that list create a string with Array.join

var products = [{"id":"xxx","name":"AAA"},{"id":"xxx","name":"BBB"}];
console.log(products.map(item => item.name).join(','))

about 4 years ago · Juan Pablo Isaza Report

0

I really don't understand your questions but i guess you're trying to concatenate the name values .If Yes,use the below code.

var result = products.map(p => Object.values(p.name).join('')).join('');
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!