I’m looking for a way to combine JSON objects if they have the same address
value in them. Building off this Combining JSON Question I have these two sample JSON
files. Would love any suggestions thank you so much.
Before Combining:
[
{
"saleNo": "86131",
"address": "6562 Dagmar Haven Suite 593 Warner Robins WI, 68085-5440",
"deliveryTo": "Earl Bruen"
},
{
"saleNo": "82483",
"address": "6562 Dagmar Haven Suite 593 Warner Robins WI, 68085-5440",
"deliveryTo": "Harold Kuhn"
},
{
"saleNo": "53731",
"address": "33194 Royal Track Suite 501 Enid CO, 57355",
"deliveryTo": "Kristopher Bayer"
},
{
"saleNo": "12285",
"address": "183 Lazaro Meadow Suite 841 Council Bluffs AL, 52499",
"deliveryTo": "Cassandra Mueller"
},
{
"saleNo": "23404",
"address": "89319 Witting Green Suite 924 Portland MN, 74633-9170",
"deliveryTo": "Chris Thiel Sr."
},
{
"saleNo": "70528",
"address": "2410 Zaria Forges Suite 936 St. Louis GA, 94962-5376",
"deliveryTo": "Glenda Larson"
}
]
After Combining:
[
{
"saleNo": ["86131", "82483"],
"address": "6562 Dagmar Haven Suite 593 Warner Robins WI, 68085-5440",
"deliveryTo": ["Harold Kuhn", "Earl Bruen"]
},
{
"saleNo": "53731",
"address": "33194 Royal Track Suite 501 Enid CO, 57355",
"deliveryTo": "Kristopher Bayer"
},
{
"saleNo": "12285",
"address": "183 Lazaro Meadow Suite 841 Council Bluffs AL, 52499",
"deliveryTo": "Cassandra Mueller"
},
{
"saleNo": "23404",
"address": "89319 Witting Green Suite 924 Portland MN, 74633-9170",
"deliveryTo": "Chris Thiel Sr."
},
{
"saleNo": "70528",
"address": "2410 Zaria Forges Suite 936 St. Louis GA, 94962-5376",
"deliveryTo": "Glenda Larson"
}
]
Sample Code:
let data = [
{
"saleNo": "86131",
"address": "6562 Dagmar Haven Suite 593 Warner Robins WI, 68085-5440",
"deliveryTo": "Earl Bruen"
},
{
"saleNo": "82483",
"address": "6562 Dagmar Haven Suite 593 Warner Robins WI, 68085-5440",
"deliveryTo": "Harold Kuhn"
},
{
"saleNo": "53731",
"address": "33194 Royal Track Suite 501 Enid CO, 57355",
"deliveryTo": "Kristopher Bayer"
},
{
"saleNo": "12285",
"address": "183 Lazaro Meadow Suite 841 Council Bluffs AL, 52499",
"deliveryTo": "Cassandra Mueller"
},
{
"saleNo": "23404",
"address": "89319 Witting Green Suite 924 Portland MN, 74633-9170",
"deliveryTo": "Chris Thiel Sr."
},
{
"saleNo": "70528",
"address": "2410 Zaria Forges Suite 936 St. Louis GA, 94962-5376",
"deliveryTo": "Glenda Larson"
}
]
let result = Object.values(data.reduce((c, {address,numbers}) => {
c[address] = c[address] || {address,numbers: []};
c[address].numbers = c[address].numbers.concat(Array.isArray(numbers) ? numbers : [numbers]);
return c;
}, {}));
console.log(result);
2
Answers
So what about
not doing anything clever? Result is not exactly the desired result, because
saleNo
anddeliveryTo
are always anArray
even if there’s just one value – you could adapt the example on first adding a string, and if it’s notundefined
, checking if it’s anArray
to simply add the new value, or otherwise replace the string with anArray
that contains the first value. Not elegant, but easy to read/follow what’s happening. Problem of course would be that on reading theresult
, you again have to handle this variance, instead of just always having anArray
.This solution uses:
This reduce and map solution is faster than the nested for loops approach for large datasets, e.g. O(2n) vs. O(n*n)