如何将交叉滤波器(即多维阵列)转换回阵列?

时间:2015-03-12 05:08:47

标签: javascript arrays json crossfilter

例如,假设我通过执行以下操作将此数组转换为交叉过滤器

var Array = [
            {Id: "01", Name: "A", Price: "1.00", Quantity: "1"},
            {Id: "02", Name: "B", Price: "10.00", Quantity: "1"},
            {Id: "04", Name: "C", Price: "9.50", Quantity: "10"},
            {Id: "03", Name: "a", Price: "9.00", Quantity: "2"},
            {Id: "06", Name: "b", Price: "100.00", Quantity: "2"},
            {Id: "05",Name: "c", Price: "1.20", Quantity: "2"}
        ];

var ndx = crossfilter(Array);

如何将它从多维数组更改回上面的原始形式,最初在数组中定义

我已尝试过以下功能

function transform_back_to_array(ndx){
var f = eval(ndx)
return JSON.stringify(f)
};

输出应该是以下内容,它应该与原始输出相同:

[
                {Id: "01", Name: "A", Price: "1.00", Quantity: "1"},
                {Id: "02", Name: "B", Price: "10.00", Quantity: "1"},
                {Id: "04", Name: "C", Price: "9.50", Quantity: "10"},
                {Id: "03", Name: "a", Price: "9.00", Quantity: "2"},
                {Id: "06", Name: "b", Price: "100.00", Quantity: "2"},
                {Id: "05",Name: "c", Price: "1.20", Quantity: "2"}
            ];

请参阅此codepen,了解我将原始数组转换回其原始形式的尝试:http://codepen.io/chriscruz/pen/MYPEwY

crossfilter tutorial中,以下函数用于打印控制台的ndx结果。我相信这有一些线索:

function print_filter(filter){
    var f=eval(filter);
    if (typeof(f.length) != "undefined") {}else{}
    if (typeof(f.top) != "undefined") {f=f.top(Infinity);}else{}
    if (typeof(f.dimension) != "undefined") {f=f.dimension(function(d) { return "";}).top(Infinity);}else{}
    console.log(filter+"("+f.length+") = "+JSON.stringify(f).replace("[","[\n\t").replace(/}\,/g,"},\n\t").replace("]","\n]"));
};

0 个答案:

没有答案