如何将JSON数组数据从一个JS函数发送到另一个?

时间:2023-01-15 20:45:52

I am calling a JS function from another and sending JSON array data. While sending the data to 2nd function I am getting value as undefined/null. How to send JSON array data from one JS function to Another ?

我从另一个函数调用一个JS函数,并发送JSON数组数据。当将数据发送到第二个函数时,我得到的值为undefined/null。如何将JSON数组数据从一个JS函数发送到另一个?

Function: 1

功能:1

$(function createJsonArray() {

            var categories =  [
                    'Jan',
                    'Feb',
                    'Mar',
                    'Apr',
                    'May',
                    'Jun',
                    'Jul',
                    'Aug',
                    'Sep',
                    'Oct',
                    'Nov',
                    'Dec'
                ];

            var confidence = [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4];

            var jsonArray = {
                issueData: []
            };

            jsonArray.issueData.push({
                "categories": categories,
                "confidence": confidence
            });
            //alert('categories: ' + jsonArray.issueData[0].categories);


            // calling function-2
            createGraph(JSON.stringify(jsonArray));
        });

Function: 2

功能:2

$(function createGraph(graphData) {
            var jsonArray = $.parseJSON(graphData);
            alert('jsonArray: ' + jsonArray);
            $('#container').highcharts({
                chart: {
                    type: 'column'
                },
                title: {
                    text: 'Monthly Average Rainfall'
                },
                subtitle: {
                    text: 'Source: WorldClimate.com'
                },
                xAxis: {
                    categories: jsonArray.issueData[0].categories,
                    crosshair: true
                },
                yAxis: {
                    min: 0,
                    title: {
                        text: 'Rainfall (mm)'
                    }
                },
                tooltip: {
                    headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
                    pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                        '<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
                    footerFormat: '</table>',
                    shared: true,
                    useHTML: true
                },
                plotOptions: {
                    column: {
                        pointPadding: 0.2,
                        borderWidth: 0
                    }
                },
                series: [{
                    name: 'Tokyo',
                    data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]

                }]
            });
        });

JSfiddle link

JSfiddle链接

1 个解决方案

#1


1  

Check this update fiddle

检查此更新小提琴

You need to invoke the createJSONArray() method and get rid of the jquery call of $(function functionName), instead just define a simple method.

您需要调用createJSONArray()方法并删除$(function functionName)的jquery调用,而只需定义一个简单的方法。

#1


1  

Check this update fiddle

检查此更新小提琴

You need to invoke the createJSONArray() method and get rid of the jquery call of $(function functionName), instead just define a simple method.

您需要调用createJSONArray()方法并删除$(function functionName)的jquery调用,而只需定义一个简单的方法。