poppyc poppyc - 17 days ago 6
Javascript Question

Create new array from elements of 2 http requests

I have 2 json arrays which are the results of 2 http.get requests.
One contains a watch list like:

json1= [
{“id”:6,”ticker”:”PG”,”gt_lt”:”<“,”price_trigger”:75},
{“id”:8,”ticker”:”T”,”gt_lt”:”<“,”price_trigger”:39.5},
{“id”:9,”ticker”:”WM”,”gt_lt”:”<“,”price_trigger”:60}
];


The second one contains stock pricing info:

json2 = [
{“symbol”:”PG”,”name”:”Procter & Gamble Company (The) “,”lastTradeDate”:null,”lastTradePriceOnly”:88.4,”change”:0.35,”dividendYield”:3.04,”peRatio”:23.93,”volume”:1333524},
{“symbol”:”T”,”name”:”AT&T Inc.”,”lastTradeDate”:null,”lastTradePriceOnly”:39.94,”change”:-0.26,”dividendYield”:4.78,”peRatio”:17.22,”volume”:2865061},
{“symbol”:”WM”,”name”:”Waste Management, Inc. Common S”,”lastTradeDate”:null,”lastTradePriceOnly”:62.98,”change”:0.17,”dividendYield”:2.61,”peRatio”:24.61,”volume”:276554}
];


I wish to create a third json array or object comprised of the first array and all elements of second array where "ticker"(json1) == "symbol"(json2).
This would allow me to utilize ngFor on the new array and get all the required info into one list or table. Any help would be greatly appreciated.

Answer

You can do it like this, easy way:

var json3 = [];

json1.forEach(function(v,i){
    json2.forEach(function(b,j){
        if(b.symbol == v.ticker)
            json3.push(Object.assign({},v,b));
    });
});