5

我现在花了整个上午都在搞砸这个并在这里阅读,但发现自己在转圈!

我正在尝试使用出色的AmChartsJavascript Charts 绘制图表,以条形图的形式显示股票持有量,以折线图的形式显示股票走势。

我无法从一个查询中获取两组数据到我的数据库,也无法使用 AmCharts StockChart,因为它不是基于时间的数据……因此,我有两组需要与 Javascript 结合的数据。

正在从数据库中提取数据并以 JSON 数组的形式成功返回,类似于:

销售数据:

[{"brandName":"Fender","gearShiftedPerMonth":"35","retailSalesPerMonth":"55"},
 {"brandName":"Gibson","gearShiftedPerMonth":"23","retailSalesPerMonth":"43"},
 {"brandName":"Epiphone","gearShiftedPerMonth":"10","retailSalesPerMonth":"13"}]

库存数据:

[{"brandName":"Gibson","stockValue":"1234"},
 {"brandName":"Fender","stockValue":"975"},
 {"brandName":"Epiphone","stockValue":"834"}]

显然,实际数字是在该示例中组成的!

现在,我需要做的是结合这些来创建这个:

组合数据

[{"brandName":"Fender","gearShiftedPerMonth":"35","retailSalesPerMonth":"55","stockValue":"975"},
 {"brandName":"Gibson","gearShiftedPerMonth":"23","retailSalesPerMonth":"43","stockValue":"1234"},
 {"brandName":"Epiphone","gearShiftedPerMonth":"10","retailSalesPerMonth":"13","stockValue":"834"}]

我们所拥有的是销售数据集与库存数据集相结合,以添加stockValue添加到相应品牌名称记录的附加数据。

我试过使用$.extend,但我不知道在这种情况下如何使用它。

可能需要注意的是,数据对可能不一定按正确的顺序排列,虽然不太可能,但可能不匹配,因此必须实现某种归零错误捕获。

4

4 回答 4

4

您首先需要做的是将数据转换为两个对象,其属性是您要合并在一起的值:

{
"Fender" : {"gearShiftedPerMonth":"35","retailSalesPerMonth":"55"},
"Gibson" : {"gearShiftedPerMonth":"23","retailSalesPerMonth":"43"},
"Epiphone" : {"gearShiftedPerMonth":"10","retailSalesPerMonth":"13"}
}

{
"Gibson": {"stockValue":"1234"},
"Fender": { "stockValue":"975"},
"Epiphone": { "stockValue":"834"}
}

转换完成后,您将拥有两个可以使用$.extend或其他功能合并的对象。

更新

对于大型集合,这会在几乎线性的时间内给出结果:

var salesa = {}, stocka = {};
$.each(sales, function(i, e) {
    salesa[e.brandName] = e;
});
$.each(stock, function(i, e) {
    stocka[e.brandName] = e;
});

var combine = {};
$.extend(true, combine, salesa, stocka)

$each(stock...如果合并发生在第二个转换回调 ( ) 期间而不是单独调用,则可以调整更快的速度,$.extend()但它失去了一些明显性。

于 2012-07-31T13:41:02.653 回答
2

如果您的示例代码反映了现实,那么 jQuery$.extend将是错误的工具。

它盲目地将数据从一个对象复制到另一个对象。请注意,您的数据顺序并不一致。SALES DATA 排在Fender第一位,而 STOCK DATA 排在gibson第一位。

所以 jQuery$.extend混合了这两个结果。的“gearShifted”和“retailSales”以Fender.的“brandName”和“stockValue”结尾Gibson


您需要的是迭代一个数组,并在另一个数组中查找“brandName”,然后复制您想要的数据。如果你愿意,你可以使用$.extend它的那部分......

var sales_data = 
[{"brandName":"Fender","gearShiftedPerMonth":"35","retailSalesPerMonth":"55"},
 {"brandName":"Gibson","gearShiftedPerMonth":"23","retailSalesPerMonth":"43"},
 {"brandName":"Epiphone","gearShiftedPerMonth":"10","retailSalesPerMonth":"13"}]

var stock_data = 
[{"brandName":"Gibson","stockValue":"1234"},
 {"brandName":"Fender","stockValue":"975"},
 {"brandName":"Epiphone","stockValue":"834"}]

var combined = $.map(sales_data, function(obj, i) {

    return $.extend({}, obj, $.grep(stock_data, function(stock_obj) {
        return obj.brandName === stock_obj.brandName
    })[0]);
});

请注意,这并不是非常有效,但除非数据集非常庞大,否则这应该不是问题。


演示:http: //jsfiddle.net/sDyKx/

结果:

[
    {
        "brandName": "Fender",
        "gearShiftedPerMonth": "35",
        "retailSalesPerMonth": "55",
        "stockValue": "975"
    },
    {
        "brandName": "Gibson",
        "gearShiftedPerMonth": "23",
        "retailSalesPerMonth": "43",
        "stockValue": "1234"
    },
    {
        "brandName": "Epiphone",
        "gearShiftedPerMonth": "10",
        "retailSalesPerMonth": "13",
        "stockValue": "834"
    }
]
于 2012-07-31T14:09:56.607 回答
2

我认为他想要做的是将两个数据集连接起来,就好像它们是表一样,通过品牌名称连接。从我一直在测试的情况来看,jQuery 的 $.extend() 函数并没有解决这个问题,而是根据对象在它接收的对象数组中的索引来合并对象。

我认为密钥的匹配需要手动完成。

stock = [{"brandName":"Fender","gearShiftedPerMonth":"35","retailSalesPerMonth":"55"},
 {"brandName":"Gibson","gearShiftedPerMonth":"23","retailSalesPerMonth":"43"},
 {"brandName":"Epiphone","gearShiftedPerMonth":"10","retailSalesPerMonth":"13"}];
value = [{"brandName":"Gibson","stockValue":"1234"},
 {"brandName":"Fender","stockValue":"975"},
 {"brandName":"Epiphone","stockValue":"834"}];

var results = [];
$(stock).each(function(){
    datum1 = this;
    $(value).each(function() {
        datum2 = this;
        if(datum1.brandName == datum2.brandName)
            results.push($.extend({}, datum1, datum2));
    });
});

这将导致:

[{"brandName":"Fender","gearShiftedPerMonth":"35","retailSalesPerMonth":"55","stockValue":"975"},
{"brandName":"Gibson","gearShiftedPerMonth":"23","retailSalesPerMonth":"43","stockValue":"1234"},
{"brandName":"Epiphone","gearShiftedPerMonth":"10","retailSalesPerMonth":"13","stockValue":"834"}]

而不是使用$.extend()返回的东西:

[{"brandName":"Gibson","gearShiftedPerMonth":"35","retailSalesPerMonth":"55","stockValue":"1234"},
{"brandName":"Fender","gearShiftedPerMonth":"23","retailSalesPerMonth":"43","stockValue":"975"},
{"brandName":"Epiphone","gearShiftedPerMonth":"10","retailSalesPerMonth":"13","stockValue":"834"}]
于 2012-07-31T14:12:48.663 回答
1

在 vanilla javascript 中,您可以执行以下操作:

var sales = [{"brandName":"Fender","gearShiftedPerMonth":"35","retailSalesPerMonth":"55"},
 {"brandName":"Gibson","gearShiftedPerMonth":"23","retailSalesPerMonth":"43"},
 {"brandName":"Epiphone","gearShiftedPerMonth":"10","retailSalesPerMonth":"13"}];

var stock = [{"brandName":"Gibson","stockValue":"1234"},
 {"brandName":"Fender","stockValue":"975"},
 {"brandName":"Epiphone","stockValue":"834"}];

var combined = stock.slice(0);     

for (var i = 0; i < stock.length; i++) {
    for (var j = 0; j < sales.length; j++) {
        if (stock[i].brandName === sales[j].brandName) {
            for (var attrname in sales[j]) { combined[i][attrname] = sales[j][attrname]; }
        }
    }
}

JSON.stringify(combined)

生产

[
{"brandName":"Gibson","stockValue":"1234","gearShiftedPerMonth":"23","retailSalesPerMonth":"43"},
{"brandName":"Fender","stockValue":"975","gearShiftedPerMonth":"35","retailSalesPerMonth":"55"},
{"brandName":"Epiphone","stockValue":"834","gearShiftedPerMonth":"10","retailSalesPerMonth":"13"}
]
于 2012-07-31T13:57:19.623 回答