仅获取对象的最后一个值 [英] Taking only last value of Object

查看:574
本文介绍了仅获取对象的最后一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现Google Graph,但是这段代码有什么问题呢,它使用的是对象的最后一个值.

I am trying to implement Google Graph but What is the wrong in this code, it is taking last value of object.

var colEvn = ["AAA","BBB","CCC"];

var viewObj = new Object();

    var func = function viewFunc(dt, row) {
        return (dt.getValue(row, 1) == colEvn[j]) ? dt.getValue(row, 2) : null;
    }
    var viewObj = new Object();

    for (var j = 0, m = colEvn.length; j < m; j++) {
        viewObj.type = 'number';
        viewObj.label = colEvn[j];
        viewObj.calc = func
     }


    var view = new google.visualization.DataView(data);
    view.setColumns([0, viewObj]);

预期结果应该是这样的:

The expected result should be like this :

{
        type: 'number',
        label: 'AAA',
        calc: function (dt, row) {
            return (dt.getValue(row, 1) == 'AAA') ? dt.getValue(row, 2) : null;
        }
    }, {
        type: 'number',
        label: 'BBB',
        calc: function (dt, row) {
            return (dt.getValue(row, 1) == 'BBB') ? dt.getValue(row, 2) : null;
        }
    }, {
        type: 'number',
        label: 'CCC',
        calc: function (dt, row) {
            return (dt.getValue(row, 1) == 'CCC') ? dt.getValue(row, 2) : null;
        }
    }

推荐答案

迭代每次都覆盖相同的对象,似乎应该将一组对象传递给可视化API.

The iteration is overwriting the same object every time, and it seems like an array of objects should be passed visualization API.

阅读文档,似乎还应该将一个对象将列的索引传递给setColumns函数,因此您可能还必须在循环中执行该操作

Reading the documentation, it also seems like an object should be passed with the index of the column to the setColumns function, so you probably have to do that in the loop as well

var view = new google.visualization.DataView(data);

for (var j = 0, m = colEvn.length; j < m; j++) {
    var viewObj = {
        type:  'number',
        label: colEvn[j],
        calc:  func
    };
    view.setColumns([j, viewObj]);
 }

这篇关于仅获取对象的最后一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆