0

I have to create an array in the following format

var myData = [
  {
    x: "10:00",
    y: [15, 30],
  },
];

Where my code looks like

myData.push({
  x: startingHoursToPush + ":00",
  y: "[" + startingHours[1] + "," + endingHours[1] + "]",
});

but the result I get is as following

x: "11:00"
y: "[15,30]"

I need to make the values for y an array instead of a string

3
  • "[" does not do what you think it does. Commented Oct 10, 2021 at 13:40
  • Yes, I am struggling to make the values of y as an array Commented Oct 10, 2021 at 13:44
  • Just use [ and ] without quotes to declare an array-literal. Commented Oct 10, 2021 at 13:44

3 Answers 3

1

use either [ ]

myData.push({
  x: startingHoursToPush + ":00",
  y: [startingHours[1],endingHours[1]],
});

DEMO FIDDLE 1

OR

use array inside

myData.push({
      x: startingHoursToPush + ":00",
      y: new Array(startingHours[1],endingHours[1]),
    });

DEMO FIDDLE 2

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

Comments

0

You are making a string instead of an array, try this instead

myData.push({
  x: startingHoursToPush + ":00",
  y: [startingHours[1], endingHours[1]],
});

Comments

0

JS is wonderful. Just wrap it in [] brackets.

myData.push({
  x: startingHoursToPush + ":00",
  y: [startingHours[1] , endingHours[1] ],
});

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.