I was trying to get the label and data value from the following
var barChartData = {
labels: Months,
datasets: [{
label: 'Dataset 1',
backgroundColor: "#09a",
data: [5, 10, 15, 20, 25, 30, 35]
}]
};
I tried using alert(JSON.stringify(barChartData.datasets.data));
but I got output as undefined. Please help me to find out this .
Like Sachin K wrote in the comment.
You forget that datasets is an array containing an object.
Therefor you need
alert(JSON.stringify(barChartData.datasets[0].data))
With the [0] you specify that you want the value from the first element in the array (array's are zero based)
Try this approach..
var barChartData = { labels: 'Months', datasets: [{ label: 'Dataset 1', backgroundColor: "#09a", data: [5, 10, 15, 20, 25, 30, 35] }] }; //For multiple dataset var data = []; label = []; barChartData.datasets.map(function(dt) { data.push(dt.data); label.push(dt.label); }) //Single datasets var data1 = barChartData.datasets[0].data; var label1 = barChartData.datasets[0].label; console.log(data, label, data1, label1);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.