Home > Net >  Convert dictionary JavaScript object to List of Items
Convert dictionary JavaScript object to List of Items

Time:01-31

I have the below Javascript object.

{
  1: {
    trade_date: "2022-12-09T00:00:00",
    settlement_date: null,
    trade_type: "Buy",
    quantity: 25,
    security_id: "AAPL",
    price: 125,
    portfolio_name: " Goldman Sachs.",
    counter_party: null,
    trader: "trader1"
  },
  2: {
    trade_date: "2022-12-09T00:00:00",
    settlement_date: null,
    trade_type: "Buy",
    quantity: 25,
    security_id: "AMZN",
    price: 105,
    portfolio_name: "JPM",
    counter_party: null,
    trader: "trader2"
  }
}

I want this to convert to as below.

Basically, 1 is trade_id. So, I want to give that name and make it as a list.

[
  {
    trade_id: 1,
    trade_date: '12/9/2022',
    settlement_date: '1/1/0001',
    trade_type: 'Buy',
    quantity: '25',
    security_id: 'AAPL',
    price: '125',
    portfolio_name: 'Goldman Sachs',
    counter_party: null,
    trader: 'trader1',
  },
  {
    trade_id: 2,
    trade_date: '12/9/2022',
    settlement_date: '1/1/0001',
    trade_type: 'Buy',
    quantity: '25',
    security_id: 'AMZN',
    price: '125',
    portfolio_name: 'JPM',
    counter_party: null,
    trader: 'trader2',
  },
];

CodePudding user response:

You can map over Object.entries.

let obj={1:{trade_date:"2022-12-09T00:00:00",settlement_date:null,trade_type:"Buy",quantity:25,security_id:"AAPL",price:125,portfolio_name:" Goldman Sachs.",counter_party:null,trader:"trader1"},2:{trade_date:"2022-12-09T00:00:00",settlement_date:null,trade_type:"Buy",quantity:25,security_id:"AMZN",price:105,portfolio_name:"JPM",counter_party:null,trader:"trader2"}};
let res = Object.entries(obj).map(([id, v]) => ({trade_id:  id, ...v}));
console.log(res);

CodePudding user response:

This is probably a good use case for Array.reduce - Iterate over the keys in the object, which allows you access each sequential list element, modify its properties, and add them to a new array.

const data = {
  1: {
    trade_date: "2022-12-09T00:00:00",
    settlement_date: null,
    trade_type: "Buy",
    quantity: 25,
    security_id: "AAPL",
    price: 125,
    portfolio_name: " Goldman Sachs.",
    counter_party: null,
    trader: "trader1"
  },
  2: {
    trade_date: "2022-12-09T00:00:00",
    settlement_date: null,
    trade_type: "Buy",
    quantity: 25,
    security_id: "AMZN",
    price: 105,
    portfolio_name: "JPM",
    counter_party: null,
    trader: "trader2"
  }
}

const reducedData = Object.keys(data).reduce((aggregate, key) => {
  const currentElement = data[key];
  currentElement["trade_id"] = parseInt(key);
  aggregate.push(currentElement);
  return aggregate;
}, []);

console.log(reducedData);

CodePudding user response:

You can use a traditional for in loop to handle this scenario.

  1. Loop over the object using for in
  2. Append the index as trade_id in the object
  3. Push the final object into an empty array

let obj = {
  1: {
    trade_date: "2022-12-09T00:00:00",
    settlement_date: null,
    trade_type: "Buy",
    quantity: 25,
    security_id: "AAPL",
    price: 125,
    portfolio_name: " Goldman Sachs.",
    counter_party: null,
    trader: "trader1"
  },
  2: {
    trade_date: "2022-12-09T00:00:00",
    settlement_date: null,
    trade_type: "Buy",
    quantity: 25,
    security_id: "AMZN",
    price: 105,
    portfolio_name: "JPM",
    counter_party: null,
    trader: "trader2"
  }
}

let result = []

for (const i in obj) {
  // Double Bitwise NOT Operator to convert string to number 
  obj[i].trade_id = ~~i;
  result.push(obj[i]);
}

  • Related