0

I have two JSON arrays with same fields as follows:

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" }
];

I need a third JSON file which has the intersection of the above two JSON files. (By intersection, I mean all the rows from both the JSON files which have TOTAL=90 AND TIP=0) Is there some way to do this?

My expected output will be a third JSON file with the following output

{"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"}
Tushar Walzade
  • 3,737
  • 4
  • 33
  • 56
Nisha Singh
  • 15
  • 2
  • 8

4 Answers4

0

You need to loop the 2 objects and merge the contents into 1 object.

For examples check this thread since this is a duplicate How can I merge properties of two JavaScript objects dynamically?

Community
  • 1
  • 1
JohanSellberg
  • 2,423
  • 1
  • 21
  • 28
0

You could do the following to collect all the rows from both the JSON files which have TOTAL = 90 and 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);

Note - this can be optimized to reduce the time complexity.

Tushar Walzade
  • 3,737
  • 4
  • 33
  • 56
-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;

use allData

Jitendra
  • 558
  • 8
  • 23
-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;
}

JSFiddle

EDIT: Update the function with the use of concat to provide a slightly more short hand.

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;
}

New JSFiddle

jhyap
  • 3,779
  • 6
  • 27
  • 47