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);

9
  • 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. Commented Apr 25, 2021 at 15:45
  • This is a good case for a debugger. Commented Apr 25, 2021 at 15:46
  • where does radius comes from? Commented Apr 25, 2021 at 15:48
  • @outis thanks for answering, yeah I have jsfiddle I have been trying to fix it for the last hour and nothing jsfiddle.net/zfo3ag08 Commented Apr 25, 2021 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 Commented Apr 25, 2021 at 15:49

2 Answers 2

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);

Sign up to request clarification or add additional context in comments.

1 Comment

This is a better, more concise solution :)
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);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.