Javascript – Conevert an object to specific array [duplicate]

I want one conversion in JavaScript. i know with mapping or reducing i can do this conversion. the following details tell you my purpose. I have one specific object like this :

let oldObject = [
    {
      signal_market: 'ENJUSDT',
      signal_initial_price: 1.33607,
      signal_last_price: 1.33607
    },
    {
      signal_market: 'ENJUSDT',
      signal_initial_price: 1.33607,
      signal_last_price: 1.33607
    },
    {
      signal_market: 'DREPUSDT',
      signal_initial_price: 0.007097,
      signal_last_price: 0.007097
    },
    {
      signal_market: 'KAVAUSDT',
      signal_initial_price: 4.1702,
      signal_last_price: 4.1702
    },
];

with the following mapping :

let newObject = oldObject.map(item => {
    const container = [];
    container[item.signal_market] = [
        item.signal_market,
        item.signal_initial_price,
        item.signal_last_price,
    ];
    return container;
});

i get the following result :

console.log(newObject);
/*
[
  [ ENJUSDT: [ 'ENJUSDT', 1.33607, 1.33607 ] ],
  [ ENJUSDT: [ 'ENJUSDT', 1.33607, 1.33607 ] ],
  [ DREPUSDT: [ 'DREPUSDT', 0.007097, 0.007097 ] ],
  [ KAVAUSDT: [ 'KAVAUSDT', 4.1702, 4.1702 ] ]
]
*/

can any body tell me with which mapping or reducing i can get finalObject object? note that the duplicated key is removed.

let finalObject = [
    'ENJUSDT': {
      signal_market: 'ENJUSDT',
      signal_initial_price: 1.33607,
      signal_last_price: 1.33607
    },
    'DREPUSDT': {
      signal_market: 'DREPUSDT',
      signal_initial_price: 0.007097,
      signal_last_price: 0.007097
    },
    'KAVAUSDT': {
      signal_market: 'KAVAUSDT',
      signal_initial_price: 4.1702,
      signal_last_price: 4.1702
    },
];


Read more here: https://stackoverflow.com/questions/67012538/javascript-conevert-an-object-to-specific-array

Content Attribution

This content was originally published by Parsika at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: