Building an Object with Arrays in matching keys

Hi, Basically I want to format an object so that if the key exist I want to push elements to that key.
I’m struggling with building Objects to format data. If anyone can point me to go exercises to do thank you.

const Manager = [
  {method: "Mint Listed", From: '2342', To:'2254s', Txhash:'fa34'},
  {method: "Atomic Match", From: 'y56', To:'566h', Txhash:'af34'},
  {method: "Close Swap Intent", From: 'h5hf5', To:'2254s', Txhash:'4tsd3'},
  {method: "Safe intent Transform", From: '5h56', To:'2254s', Txhash:'354'},
  {method: "Mint Listed", From: 'j65', To:'sdfc', Txhash:'fa3'},
  {method: "Mint Listed", From: '53f', To:'dfg', Txhash:'f34'},
  {method: "Proxy match", From: 'asd', To:'765', Txhash:'3f4'},
  {method: "Proxy match", From: '000', To:'xdfs', Txhash:'24trr'},
  {method: "Proxy match", From: '111111', To:'xcf8', Txhash:'a43'},
]

const hash = {}
let arr = []
const play = Manager.map(d => {
  // add the key to hash 
  hash[ d['method']] = elements(d)
  return hash
}) 

function elements(data){
  return {
    from: data.From,
    To: data.To,
    Txhash: data.Txhash
  }
}

console.log(hash)

repo link
https://replit.com/@KuKoVisuals1/object-format?v=1

could you try to be more clear on what you want to accomplish? I don’t think I understood what’s your desired output

The output will be something like this

{
  'Mint Listed': [],
  'Atomic Match': [],
  'Close Swap Intent': [],
  'Safe intent Transform': [],
  'Proxy match': []
}

with their respective elements from each key, I don’t know how to push the elements of their matching keys e.i proxy match should have two arrays

this is almost working but the values are nested

const Manager = [
  {method: "Mint Listed", From: '2342', To:'2254s', Txhash:'fa34'},
  {method: "Atomic Match", From: '232f', To:'566h', Txhash:'af34'},
  {method: "Close Swap Intent", From: '#%#%@', To:'2254s', Txhash:'4tsd3'},
  {method: "Safe intent Transform", From: 'SSZZZ', To:'2254s', Txhash:'354'},
  {method: "Mint Listed", From: 'ew2', To:'sdfc', Txhash:'fa3'},
  {method: "Mint Listed", From: '53f', To:'dfg', Txhash:'f34'},
  {method: "Proxy match", From: 'asd', To:'765', Txhash:'3f4'},
  {method: "Proxy match", From: '000', To:'xdfs', Txhash:'24trr'},
  {method: "Proxy match", From: '111111', To:'xcf8', Txhash:'a43'},
]

const hash = {}
let arr = []
const play = Manager.map(d => {
  // add the key to hash 
  
  const descriptor1 = Object.getOwnPropertyDescriptor(hash,  d['method']);

  hash[ d['method']] = Object.assign({}, descriptor1, elements(d))  
  
  return hash
}) 

function elements(data){
  return {
    from: data.From,
    To: data.To,
    Txhash: data.Txhash
  }
}

console.log(hash)

found the answer 1 line of code in d3js

const methods = d3.group(data, d => d.Method)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.