Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

78
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda