Home > database >  In array of multiple objects and key value pairs, convert all number values to negative
In array of multiple objects and key value pairs, convert all number values to negative

Time:05-20

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]))

CodePudding user response:

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)

CodePudding user response:

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)

  • Related