I have 3 similar javascript objects.
var gdp = { "city": city, "gdp": [], }; var income = { "city": city, "income": [], }; var uRate = { "city": city, "uRate": [], };
Where I have many cities (n = 28), and [] is my array of integer data with the same length for each: gdp, income, uRate .
PURPOSE: Combine them into one object for each city:
var finalData = { "city": city, "gdp": [], "income": [], "uRate": [] }
I tried the options for the following.
if ( ObjA.city == ObjB.city ) { Obj.city.metric = this.metric] };
Or, if the city ββmatches , add a label (gdp, income, or uRate) to the finalData object.
cities.forEach(function ( city ) { var metrics = ['gdp', 'income', 'uRate']; metrics.forEach(function ( metric ) { if ( metric.city == city ) {
Here is an example using $ .extend () with jQuery: How to combine two values ββof an object by keys , but I do not use jQuery and prefer either lodash, d3.js or vanilla js.
Thank.
DeBraid Apr 20 '14 at 18:35 2014-04-20 18:35
source share