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

134
Views
Putting multiple values as single strings into object

I have an object values:

values = [{
    stringValues: "First value",
    kind: "stringValues"
  },
  {
    stringValues: "Second Value",
    kind: "stringValues"
  },
]

I need to extract the stringValues and put them into another object obj as a value to the key ghi. The final result should look like that:

{
  "name": "name",
  "abc": {
    "def": "def",
    "ghi": ["First Value", "Second Value"]
  }
}

My approach is :

var valuesStr = "";
values.forEach(
  (v) => {
    valuesStr += `'${v.stringValues}',`
  }
);

obj = {
  name: "name",
  abc: {
    def: "def",
    ghi: valuesStr,
  },
};

But the result doesn't look quite right:

{
  "name": "name",
  "abc": {
    "def": "def",
    "ghi": "'First value','Second Value',"
  }
}

As you can see, it puts both values as 1 string.

Fiddle: https://jsfiddle.net/zrx0sp76/

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

0

Well if you want it be an array, then declare it as an array and push() values to it.

var valuesArr = [];
values.forEach(
  (v) => {
    valuesArr.push(v.stringValues);
  }
);

obj = {
  name: "name",
  abc: {
    def: "def",
    ghi: valuesArr,
  },
};
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!