我有两个具有相同字段的JSON数组如下:
var total_90 = [
{ "date": "2011-11-14T17:22:59Z", "quantity": 2, "total": 90, "tip": 0, "type": "tab" },
{ "date": "2011-11-14T17:07:21Z", "quantity": 2, "total": 90, "tip": 1, "type": "tab" },
{ "date": "2012-11-14T16:30:43Z", "quantity": 3, "total": 90, "tip": 0, "type": "tab" }
];
var tip_0 = [
{ "date": "2011-11-14T17:22:59Z", "quantity": 2, "total": 80, "tip": 0, "type": "tab" },
{ "date": "2011-11-14T17:07:21Z", "quantity": 2, "total": 70, "tip": 0, "type": "tab" },
{ "date": "2011-11-14T16:58:03Z", "quantity": 2, "total": 90, "tip": 0, "type": "tab" },
{ "date": "2011-11-14T16:30:43Z", "quantity": 2, "total": 90, "tip": 0, "type": "tab" }
];
我需要第三个JSON文件,它具有上述两个JSON文件的交集。 (通过交集,我的意思是来自两个JSON文件的所有行,其中 TOTAL = 90 AND TIP = 0 ) 有没有办法做到这一点?
我的预期输出将是具有以下输出的第三个JSON文件
{"date":"2012-11-14T16:30:43Z","quantity":3,"total":90,"tip":0,"type":"tab"},
{"date":"2011-11-14T16:58:03Z","quantity":2,"total":90,"tip":0,"type":"tab"},
{"date":"2011-11-14T16:30:43Z","quantity":2,"total":90,"tip":0,"type":"tab"}
答案 0 :(得分:0)
您需要循环2个对象并将内容合并到1个对象中。
例如,检查此主题,因为这是重复的How can I merge properties of two JavaScript objects dynamically?
答案 1 :(得分:0)
您可以执行以下操作以从JSON文件中收集所有行,这些文件的 TOTAL = 90 和 TIP = 0 -
var total_90 = [
{ "date": "2011-11-14T17:22:59Z", "quantity": 2, "total": 90, "tip": 0, "type": "tab" },
{ "date": "2011-11-14T17:07:21Z", "quantity": 2, "total": 90, "tip": 1, "type": "tab" },
{ "date": "2012-11-14T16:30:43Z", "quantity": 3, "total": 90, "tip": 0, "type": "tab" }
];
var tip_0 = [
{ "date": "2011-11-14T17:22:59Z", "quantity": 2, "total": 80, "tip": 0, "type": "tab" },
{ "date": "2011-11-14T17:07:21Z", "quantity": 2, "total": 70, "tip": 0, "type": "tab" },
{ "date": "2011-11-14T16:58:03Z", "quantity": 2, "total": 90, "tip": 0, "type": "tab" },
{ "date": "2011-11-14T16:30:43Z", "quantity": 2, "total": 90, "tip": 0, "type": "tab" }
];
// An empty arrays to contain final intersection array
var result = [];
/* now looping over both arrays to traverse all the elements from them */
// iterating over first array
total_90.forEach(x => {
// iterating over second array
tip_0.forEach(y => {
// push into output array only if total is 90 & tip is 0
if ((x.total == 90 && y.total == 90) && (x.tip == 0 && y.tip == 0)) {
result.push({
date: x.date,
quantity: x.quantity,
total: x.total,
tip: x.tip,
type: x.type
});
}
});
});
console.log(result);
注意- 可以对其进行优化以降低时间复杂度。
答案 2 :(得分:-1)
var total_90 = [
{"date":"2011-11-14T17:22:59Z","quantity":2,"total":90,"tip":0,"type":"tab"},
{"date":"2011-11-14T17:07:21Z","quantity":2,"total":90,"tip":1,"type":"tab"},
{"date":"2012-11-14T16:30:43Z","quantity":3,"total":90,"tip":0,"type":"tab"}
]
var tip_0 = [
{"date":"2011-11-14T17:22:59Z","quantity":2,"total":80,"tip":0,"type":"tab"},
{"date":"2011-11-14T17:07:21Z","quantity":2,"total":70,"tip":0,"type":"tab"},
{"date":"2011-11-14T16:58:03Z","quantity":2,"total":90,"tip":0,"type":"tab"},
{"date":"2011-11-14T16:30:43Z","quantity":2,"total":90,"tip":0,"type":"tab"}
]
allData['total_90'] = total_90;
allData['tip_0'] = tip_0;
使用allData
答案 3 :(得分:-1)
function intersection(a, b)
{
var result = [];
for (var i = 0; i < a.length; i++){
if (a[i].total == 90 && a[i].tip == 0)
{
result.push(a[i]);
}
}
for (var i = 0; i < b.length; i++){
if (b[i].total == 90 && b[i].tip == 0)
{
result.push(b[i]);
}
}
return result;
}
编辑:使用concat
更新该功能,以提供更短的时间。
function intersection(a, b)
{
var result = [];
var c = a.concat(b);
for (var i = 0; i < c.length; i++){
if (c[i].total == 90 && c[i].tip == 0)
{
result.push(c[i]);
}
}
return result;
}