0

I'm trying to add a common array of objects, should be similar fund_asset_class_focus and year, and have to format into an array of arrays as below(["fund_asset..","2017-total allocation",2018,2019]).

data = [
  {
    "allocation": 60.30,
    "dt": "2017",
    "fund_asset_class_focus": "U.S. Fixed Income"
  },
  {
    "allocation": 50,
    "dt": "2017",
    "fund_asset_class_focus": "U.S. Fixed Income"
  }
  {
    "allocation": 39.80,
    "dt": "2018",
    "fund_asset_class_focus": "U.S. Fixed Income"
  },
  {
    "allocation": 40.7,
    "dt": "2019",
    "fund_asset_class_focus": "U.S. Fixed Income"
  },
  {
    "allocation": 20,
    "dt": "2018",
    "fund_asset_class_focus": "Cash"
  },
  {
    "allocation": 9.7,
    "dt": "2018",
    "fund_asset_class_focus": "Cash"
  }
  {
    "allocation": 16,
    "dt": "2018",
    "fund_asset_class_focus": "Credit"
  },
  {
    "allocation": 16,
    "dt": "2019",
    "fund_asset_class_focus": "Credit"
  },
  {
    "allocation": 16,
    "dt": "2019",
    "fund_asset_class_focus": "Credit"
  }
]

data = [["U.S. Fixed Income",110.30,39.80, 40.7],["Cash",0,29.7,0],["Credit",0,16,32]]

I tried to solve this by using the following code, however, it was unsuccessful in meeting my requirements:

for (let i in data) {
  var tempObj = {};
  if (obj[years[0]]) 
    tempObj[years[0]] = obj[years[0]];
  else
    tempObj[years[0]] = {};

  if (obj[data[i]["fund_asset_class_focus"]]) {

  }
}
4
  • 1
    1) What have you tried so far? 2) Why do you want to use this output format? It seems to not be very optimal. Commented Jul 15, 2019 at 8:42
  • Please add the code you've tried so far
    – adiga
    Commented Jul 15, 2019 at 8:43
  • I am working on the skittle chart, where data to be an array of arrays
    – kiran
    Commented Jul 15, 2019 at 8:51
  • Can you please include a more detailed desired outcome?
    – Kobe
    Commented Jul 15, 2019 at 9:02

2 Answers 2

2

Create a mapping object called years which will have each year as key and the year's position in the output array as it's value. Here, I'm assuming you have only 3 years. You could also make this dynamic. Then use reduce. Create an accumulator object with each fund_asset_class_focus as key the nested array you need in the output as value.

const data=[{allocation:60.3,dt:"2017",fund_asset_class_focus:"U.S. Fixed Income"},{allocation:50,dt:"2017",fund_asset_class_focus:"U.S. Fixed Income"},{allocation:39.8,dt:"2018",fund_asset_class_focus:"U.S. Fixed Income"},{allocation:40.7,dt:"2019",fund_asset_class_focus:"U.S. Fixed Income"},{allocation:20,dt:"2018",fund_asset_class_focus:"Cash"},{allocation:9.7,dt:"2018",fund_asset_class_focus:"Cash"},{allocation:16,dt:"2018",fund_asset_class_focus:"Credit"},{allocation:16,dt:"2019",fund_asset_class_focus:"Credit"},{allocation:16,dt:"2019",fund_asset_class_focus:"Credit"}];

const years = { 2017: 1, 2018: 2, 2019: 3 },
      defaultValues = Object.keys(years).map(_ => 0);

const merged = data.reduce((r, o) => {
  const { allocation, dt, fund_asset_class_focus: fund } = o;
  r[fund] = r[fund] || [fund, ...defaultValues];
  r[fund][years[dt]] += allocation;
  return r;
}, {})

const output = Object.values(merged);

console.log(JSON.stringify(output))

1
1

Sorted it out using lodash. Please use the below code.

var data = [
  {
    "allocation": 60.30,
    "dt": "2017",
    "fund_asset_class_focus": "U.S. Fixed Income"
  },
  {
    "allocation": 50,
    "dt": "2017",
    "fund_asset_class_focus": "U.S. Fixed Income"
  },
  {
    "allocation": 39.80,
    "dt": "2018",
    "fund_asset_class_focus": "U.S. Fixed Income"
  },
  {
    "allocation": 40.7,
    "dt": "2019",
    "fund_asset_class_focus": "U.S. Fixed Income"
  },
  {
    "allocation": 20,
    "dt": "2018",
    "fund_asset_class_focus": "Cash"
  },
  {
    "allocation": 9.7,
    "dt": "2018",
    "fund_asset_class_focus": "Cash"
  },
  {
    "allocation": 16,
    "dt": "2018",
    "fund_asset_class_focus": "Credit"
  },
  {
    "allocation": 16,
    "dt": "2019",
    "fund_asset_class_focus": "Credit"
  },
  {
    "allocation": 16,
    "dt": "2019",
    "fund_asset_class_focus": "Credit"
  }
];

var years = _.uniqBy(data, 'dt').map(obj => obj.dt); // Get all available years ["2017", "2018", "2019"]

var result = _(data).groupBy('fund_asset_class_focus').map((objs, key) => [
  key,
  ...years.map(year => _.sumBy(_.filter(objs, obj => obj.dt === year), 'allocation'))
])
.value();

console.log(result); // [["U.S. Fixed Income",110.30,39.80, 40.7],["Cash",0,29.7,0],["Credit",0,16,32]]
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.14/lodash.min.js"></script>

3
  • can you guide me any tutorials for lodash.
    – kiran
    Commented Jul 15, 2019 at 10:57
  • 1
    Go to lodash.com. It has tons of JavaScript functions for arrays and objects. You can just include the lodash JS library into your html. <script src="cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.14/…> You can use npm lib if you use ES6
    – polyglot
    Commented Jul 15, 2019 at 12:20
  • Hi Kiran, can you please upvote the answer or mark it as accepted if you don't have any other question?
    – polyglot
    Commented Jul 15, 2019 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.