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

149
Vistas
In array of multiple objects and key value pairs, convert all number values to negative
let array = [ {2008: 234, 2009: 234, 2010: 234, 2011: 234, 2012: 234, Type: 'A', ID: 'A1'},
                    {2008: 237, 2009: 243, 2010: 325, 2011: 378, 2012: 375, Type: 'B', ID: 'B1'},
                    {2008: 172, 2009: 191, 2010: 290, 2011: 274, 2012: 305, Type: 'C', ID: 'C1'},
                    {2008: 569, 2009: 625, 2010: 713, 2011: 655, 2012: 704, Type: 'D', ID: 'D1'},
                    {2008: 116, 2009: 123, 2010: 160, 2011: 880, 2012: 215, Type: 'E', ID: 'E1'},
                    {2008: 626, 2009: 611, 2010: 805, 2011: 918, 2012: 104, Type: 'F', ID: 'F1'} ]

For the following array of objects I am trying to convert all the numbers to negative. What I would like to achieve is ...

let newArray = [ {2008: -234, 2009: -234, 2010: -234, 2011: -234, 2012: -234, Type: 'A', ID: 'A1'},
                    {2008: -237, 2009: -243, 2010: -325, 2011: -378, 2012: -375, Type: 'B', ID: 'B1'},
                    {2008: -172, 2009: -191, 2010: -290, 2011: -274, 2012: -305, Type: 'C', ID: 'C1'},
                    {2008: -569, 2009: -625, 2010: -713, 2011: -655, 2012: -704, Type: 'D', ID: 'D1'},
                    {2008: -116, 2009: -123, 2010: -160, 2011: -880, 2012: -215, Type: 'E', ID: 'E1'},
                    {2008: -626, 2009: -611, 2010: -805, 2011: -918, 2012: -104, Type: 'F', ID: 'F1'} ]

How would I do this without effecting the last 2 key/value pairs. So my far I am stuck on this ...

const newArray = array.map(( { Type, ID, ...years }) => {
        return years
    })

const newArray2 = Object.fromEntries(
        Object.entries(newArray).map(([key, value]) => [key, value * -1]))
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

One way is to check the type of the value while iterating:

let array = [{ 2008: 234, 2009: 234, 2010: 234, 2011: 234, 2012: 234, Type: 'A', ID: 'A1' }, { 2008: 237, 2009: 243, 2010: 325, 2011: 378, 2012: 375, Type: 'B', ID: 'B1' }, { 2008: 172, 2009: 191, 2010: 290, 2011: 274, 2012: 305, Type: 'C', ID: 'C1' }, { 2008: 569, 2009: 625, 2010: 713, 2011: 655, 2012: 704, Type: 'D', ID: 'D1' }, { 2008: 116, 2009: 123, 2010: 160, 2011: 880, 2012: 215, Type: 'E', ID: 'E1' }, { 2008: 626, 2009: 611, 2010: 805, 2011: 918, 2012: 104, Type: 'F', ID: 'F1' }];


const result = array.map(o =>
  Object.fromEntries(Object.entries(o).map(([k, v]) => (
    typeof v === 'number' ? [k, v *= -1] : [k, v]
  )))
);

console.log(result);

I'm assuming you want to keep non-number key/value pairs in the result based on your provided example (though your code shows you removing them)

about 4 years ago · Juan Pablo Isaza Denunciar

0

you were almost there

try this

let array = [ {2008: 234, 2009: 234, 2010: 234, 2011: 234, 2012: 234, Type: 'A', ID: 'A1'},
                    {2008: 237, 2009: 243, 2010: 325, 2011: 378, 2012: 375, Type: 'B', ID: 'B1'},
                    {2008: 172, 2009: 191, 2010: 290, 2011: 274, 2012: 305, Type: 'C', ID: 'C1'},
                    {2008: 569, 2009: 625, 2010: 713, 2011: 655, 2012: 704, Type: 'D', ID: 'D1'},
                    {2008: 116, 2009: 123, 2010: 160, 2011: 880, 2012: 215, Type: 'E', ID: 'E1'},
                    {2008: 626, 2009: 611, 2010: 805, 2011: 918, 2012: 104, Type: 'F', ID: 'F1'} ]

const result = array.map(({
    Type,
    ID,
    ...years
  }) => ({Type, ID, ...Object.fromEntries(Object.entries(years).map(([k, n]) => [k, -n]))})

)

console.log(result)

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