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

77
Views
Adding array of objects to object

I am trying to return some data as JSON.

I have an array of values:

[
  { FieldValue: '102969', count: 1 },
  { FieldValue: 'DBFL', count: 26 },
  { FieldValue: 'Daniel', count: 1 },
  { FieldValue: 'KNI', count: 9 },
  { FieldValue: 'ON', count: 895 },
  { FieldValue: 'Ole', count: 4 },
  { FieldValue: 'TNJ', count: 133 }
]

That I am trying to add to an object, in order to get an object with a fieldname, and a values array

const objectToReturn = {
      FieldName: row.FieldName,
      FieldValues: []
    };
objectToReturn.FieldValues.push(values);

returnArr.push(objectToReturn);
console.log(returnArr);

This gives

[
  { FieldName: 'Customer ID', FieldValues: [ [Array] ] },
  { FieldName: 'Order No', FieldValues: [ [Array] ] },
  { FieldName: 'Technician ID', FieldValues: [ [Array] ] }
]

I've tried using JSON.stringify(values) in stead, giving me this return

{
    FieldName: 'Technician ID',
    FieldValues: [
      '[{"FieldValue":"102969","count":1},{"FieldValue":"DBFL","count":26},{"FieldValue":"Daniel","count":1},{"FieldValue":"KNI","count":9},{"FieldValue":"ON","count":895},{"FieldValue":"Ole","count":4},{"FieldValue":"TNJ","count":133}]'
    ]
  }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

you can use spread operator

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#description

for example:

FieldValues: [...values]

or

objectToReturn.FieldValues.push(...[values]);
about 4 years ago · Juan Pablo Isaza Report

0

You need to write something like this,

const objectToReturn = {
      FieldName: row.FieldName,
      FieldValues: [...values]
    };
objectToReturn.FieldValues.push(values);

returnArr.push(objectToReturn);
console.log(returnArr);

I don't agree with other answers as they will have the object reference problem, I assume you need to have a copy of values array not the array itself

about 4 years ago · Juan Pablo Isaza Report

0

Hi you can try to do this :

objectToReturn.FieldValues = values

but you need to declare your object as a variable

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!