1

I'm trying to combine two arrays in a specific format, but I cant imagine what I'm doing wrong.

This is the output result that I would like to have:

[ data: [
  {value: 100, name: 'January'},
  {value: 30, name: 'February'},
  {value: 150, name: 'March'},
  {value: 85, name: 'April'},
  {value: 60, name: 'May'},
  {value: 20, name: 'June'}
  ],
  radius: '50%' ]

This is my code:

    var sales = ["100", "30", "150", "85", "60", "20"];
    var months = ["January", "February", "March", "April", "May", "June"];
    var rad = "50%";

    var combined = sales.map(function combine(dataItem, index) {
          return {
              data: [{"value":dataItem, "name":months[index]}],
              radius: rad
          };
                }).filter(function removeEmpty(item) {
                  return item.data.length;
                });
                
    console.log(combined);
10 Rep
  • 2,217
  • 7
  • 19
  • 33
CaesarKvs
  • 13
  • 3
  • 1
    _"This is the output result that I would like to have"_ - That would be possible, but you shouldn't misuse an array when you want an object. – Andreas Apr 25 '21 at 15:45
  • This is a good case for a debugger. – outis Apr 25 '21 at 15:46
  • where does `radius ` comes from? – DecPK Apr 25 '21 at 15:48
  • @outis thanks for answering, yeah I have jsfiddle I have been trying to fix it for the last hour and nothing https://jsfiddle.net/zfo3ag08/ – CaesarKvs Apr 25 '21 at 15:48
  • @SAM hello, radius will be always 50%, it's "defined value" that needs to be inside the object. However, edited the snippet. Thx – CaesarKvs Apr 25 '21 at 15:49
  • instead use `var combined = { data: [], radius: '50%' };` and then set `combined.data = sales.map(function (dataItem, index) { return { 'value': dataItem, 'name': months[index] } })` – Filip Kováč Apr 25 '21 at 15:49
  • Why is the intended enclosing structure an array instead of an object? If it's an object then you can just `.map` to its `data` property (instead of to the object itself). But it's not clear to me what the intent of this result is. – David Apr 25 '21 at 15:49
  • @CaesarKvs The data structure that contains `data` and `radius` should be an `object` not an arra`y` – DecPK Apr 25 '21 at 15:54
  • @CaesarKvs: if you mean you're using the fiddle to debug, it's not a debugger; you should do this in-browser (as mentioned in another [recent answer](https://stackoverflow.com/a/67255229/90527)). That way, you can see precisely how your code runs, and why it doesn't behave as you expect. Also relevant: "[Length of a JavaScript object](https://stackoverflow.com/q/5223/90527)". – outis Apr 26 '21 at 23:20

2 Answers2

3

Just map over it and add the value and name in the object.

var sales = ["100", "30", "150", "85", "60", "20"];
var months = ["January", "February", "March", "April", "May", "June"];

const data = sales.map((sale, i) => ({
  value: parseInt(sale),
  name: months[i],
}));

const result = {
  data,
  radius: "50%",
};

console.log(result);
DecPK
  • 24,537
  • 6
  • 26
  • 42
0

So, in your expected output, you are treating an array like an object. Try this for your expected result:

{data: [
  {value: 100, name: 'January'},
  {value: 30, name: 'February'},
  {value: 150, name: 'March'},
  {value: 85, name: 'April'},
  {value: 60, name: 'May'},
  {value: 20, name: 'June'}
],
radius: '50%'}

As to how to achieve the result, try this:

const sales = ["100", "30", "150", "85", "60", "20"],
months = ["January", "February", "March", "April", "May", "June"];

const combine = (s, m) => {
  const arr = [];
  for (let i = 0; i < Math.min(s.length, m.length); i++)
    arr.push({ value: s[i], name: m[i] });
  return arr;
};

const dataObj = {
  data: combine(sales, months),
  radius: "50%",
};


console.log(dataObj);
jrob11
  • 315
  • 1
  • 9