简体   繁体   English

Javascript变量在obejct内部不起作用。 但是进入console.log

[英]Javascript variable not working inside obejct . But getting on console.log

Javascript variable does not work inside an object. Javascript变量在对象内部不起作用。 I see the data when I console.log(dataPondsRevenue) variable, but getting the error: 我在console.log(dataPondsRevenue)变量时看到数据,但收到错误:

SyntaxError: missing ] after element list`! 语法错误:元素列表后缺少]!

when I use it inside the data:[] node: 当我在data:[]节点内使用它时:

$('.pondsRevenueBlock').on('click',function(){
    var block_id = $(this).attr('data-id');
    var url='{{ route('WhiteFish.client.pondsRevenueBlockWise') }}';
    $.ajax({
        url:url+'?block_id='+block_id,
    }).done(function(pondsRevenueData){
        var dataPondsRevenue = '';
        $.each(pondsRevenueData, function(index, element) {
            dataPondsRevenue+= '{value:'+element.pondTotalInvest+',name:'+element.name+'},';
        });
        console.log(dataPondsRevenue);

        var eChart_2 = echarts.init(document.getElementById('pondsRevenue'));
        var option1 = {
            tooltip : {
                backgroundColor: 'rgba(33,33,33,1)',
                borderRadius:0,
                padding:10,
                axisPointer: {
                    type: 'cross',
                    label: {
                        backgroundColor: 'rgba(33,33,33,1)'
                    }
                },
                textStyle: {
                    color: '#fff',
                    fontStyle: 'normal',
                    fontWeight: 'normal',
                    fontFamily: "'Open Sans', sans-serif",
                    fontSize: 12
                }   
            },
            // color: ['#0FC5BB', '#92F2EF', '#D0F6F5'],
            color: ['#0FC5BB', '#0FC5BB', '#5AC4CC'],
            series : [
                {
                    name: 'task',
                    type: 'pie',
                    radius : '55%',
                    center: ['50%', '50%'],
                    roseType : 'radius',
                    tooltip : {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)",
                        backgroundColor: 'rgba(33,33,33,1)',
                        borderRadius:0,
                        padding:10,
                        textStyle: {
                            color: '#fff',
                            fontStyle: 'normal',
                            fontWeight: 'normal',
                            fontFamily: "'Open Sans', sans-serif",
                            fontSize: 12
                        }   
                    },
                    data:[
                        console.log(dataPondsRevenue);
                    ],
                    itemStyle: {
                        emphasis: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ]
        };
        eChart_2.setOption(option1);
        eChart_2.resize();
    }).fail(function (data) {
        console.log('error');
    });
});

How can I solve it? 我该如何解决?

This is because console.log(dataPondsRevenue) is a function that returns undefined , so 这是因为console.log(dataPondsRevenue)是一个返回undefined的函数,所以

data: [ console.log(dataPondsRevenue) ]

means 手段

data: [ undefined ]

You should do 你应该做

data: [ dataPondsRevenue ]

to get the actual data into the array. 将实际数据放入数组。

Use an array (and optionally JSON.stringify it; in case of $.ajax , have a look at contentType at $.ajax docs ), it is much less error-prone - in your case, there's always a trailing comma at the end, which is not a valid JSON: 使用数组(以及可选的JSON.stringify ;如果是$.ajax ,请在$ .ajax docs上查看contentType ),它不那么容易出错-在您的情况下,末尾总是逗号,这不是有效的JSON:

 console.log("Valid:") console.log(JSON.parse('{ "whatever": 1 }')) console.log("Invalid:") console.log(JSON.parse('{ "whatever": 1, }')) 

$('.pondsRevenueBlock').on('click',function(){
    var block_id = $(this).attr('data-id');
    var url='{{ route('WhiteFish.client.pondsRevenueBlockWise') }}';
    $.ajax({
        url:url+'?block_id='+block_id,
        contentType: 'application/json'
    }).done(function(pondsRevenueData){
        var dataPondsRevenue = [];
        $.each(pondsRevenueData, function(index, element) {
            dataPondsRevenue.push({
              value: element.pondTotalInvest,
              name: element.name
            })
        });
        console.log(dataPondsRevenue);

        var eChart_2 = echarts.init(document.getElementById('pondsRevenue'));
        var option1 = {
            tooltip : {
                backgroundColor: 'rgba(33,33,33,1)',
                borderRadius:0,
                padding:10,
                axisPointer: {
                    type: 'cross',
                    label: {
                        backgroundColor: 'rgba(33,33,33,1)'
                    }
                },
                textStyle: {
                    color: '#fff',
                    fontStyle: 'normal',
                    fontWeight: 'normal',
                    fontFamily: "'Open Sans', sans-serif",
                    fontSize: 12
                }   
            },
            // color: ['#0FC5BB', '#92F2EF', '#D0F6F5'],
            color: ['#0FC5BB', '#0FC5BB', '#5AC4CC'],
            series : [
                {
                    name: 'task',
                    type: 'pie',
                    radius : '55%',
                    center: ['50%', '50%'],
                    roseType : 'radius',
                    tooltip : {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)",
                        backgroundColor: 'rgba(33,33,33,1)',
                        borderRadius:0,
                        padding:10,
                        textStyle: {
                            color: '#fff',
                            fontStyle: 'normal',
                            fontWeight: 'normal',
                            fontFamily: "'Open Sans', sans-serif",
                            fontSize: 12
                        }   
                    },
                    data: JSON.stringify(dataPondsRevenue),
                    itemStyle: {
                        emphasis: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ]
        };
        eChart_2.setOption(option1);
        eChart_2.resize();
    }).fail(function (data) {
        console.log('error');
    });
});

Also, the console.log() , returns undefined - you can just pass the variable instead. 另外, console.log()返回undefined ,您可以只传递变量。

There seems to be quite a confusion as to how to create the needed data object. 关于如何创建所需的数据对象,似乎存在很大的困惑。 With the code 用代码

$.each(pondsRevenueData, function(index, element) {
    dataPondsRevenue+= '{value:'+element.pondTotalInvest+',name:'+element.name+'},';
});

You are creating a JSON string . 您正在创建JSON 字符串 This could be parsed into an object using JSON.parse() but that seems unnecessary over complication as you could create the required array of objects to start with: 可以使用JSON.parse()将其解析为一个对象,但是由于复杂性,这似乎不必要,因为您可以创建所需的对象数组以以下内容开头:

var dataPondsRevenue = [];
$.each(pondsRevenueData, function(index, element) {
    dataPondsRevenue.push({value: element.pondTotalInvest, name: element.name});
});

Then, just assign dataPondsRevenue to data : 然后,只需将dataPondsRevenue分配给data

...
},
data: dataPondsRevenue,
itemStyle:
...

You are creating a JSON string. 您正在创建JSON字符串。 This could be parsed into an object using JSON.parse() but that seems unnecessary over complication as you could create the required array of objects to start with: 可以使用JSON.parse()将其解析为一个对象,但是由于复杂性,这似乎不必要,因为您可以创建所需的对象数组以以下内容开头:

var dataPondsRevenue = [];
$.each(pondsRevenueData, function(index, element) {
   dataPondsRevenue.push({value: element.pondTotalInvest, name: element.name});
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM