How to merge/concatenate values of same object properties in an array of objects using lodash?
I have an array of arrays of objects which looks like this:
let fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
I want to reduce and merge the above array using lodash into one so that the samples get concatenated together like so:
fruitSamples = [
{'id': 1, 'type': 'apples', 'samples': [1,2,3,5,2,9,11,2,33]},
{'id': 2, 'type': 'bananas', 'samples': [1,2,7,1,7,7,17,2,67]},
{'id': 3, 'type': 'pears', 'samples': [1,2,3,12,21,32,91,22,34]},
]
I have tried many different approaches but since I want the shortest possible way of solving this what would be your recommendations?
I have tried this:
let test = _(fruitSamples)
.flatten()
.groupBy('type')
.map(_.spread(_.merge))
.value();
console.log(test);
This gives me the following result, which does not concatenate the samples:
test = [
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
I feel using _.mergeWith might be the right answer, if so, I am looking for help implementing mergeWith in the best possible way as I am not sure how to do it. Any suggestions?
javascript arrays lodash javascript-objects
add a comment |
I have an array of arrays of objects which looks like this:
let fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
I want to reduce and merge the above array using lodash into one so that the samples get concatenated together like so:
fruitSamples = [
{'id': 1, 'type': 'apples', 'samples': [1,2,3,5,2,9,11,2,33]},
{'id': 2, 'type': 'bananas', 'samples': [1,2,7,1,7,7,17,2,67]},
{'id': 3, 'type': 'pears', 'samples': [1,2,3,12,21,32,91,22,34]},
]
I have tried many different approaches but since I want the shortest possible way of solving this what would be your recommendations?
I have tried this:
let test = _(fruitSamples)
.flatten()
.groupBy('type')
.map(_.spread(_.merge))
.value();
console.log(test);
This gives me the following result, which does not concatenate the samples:
test = [
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
I feel using _.mergeWith might be the right answer, if so, I am looking for help implementing mergeWith in the best possible way as I am not sure how to do it. Any suggestions?
javascript arrays lodash javascript-objects
add a comment |
I have an array of arrays of objects which looks like this:
let fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
I want to reduce and merge the above array using lodash into one so that the samples get concatenated together like so:
fruitSamples = [
{'id': 1, 'type': 'apples', 'samples': [1,2,3,5,2,9,11,2,33]},
{'id': 2, 'type': 'bananas', 'samples': [1,2,7,1,7,7,17,2,67]},
{'id': 3, 'type': 'pears', 'samples': [1,2,3,12,21,32,91,22,34]},
]
I have tried many different approaches but since I want the shortest possible way of solving this what would be your recommendations?
I have tried this:
let test = _(fruitSamples)
.flatten()
.groupBy('type')
.map(_.spread(_.merge))
.value();
console.log(test);
This gives me the following result, which does not concatenate the samples:
test = [
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
I feel using _.mergeWith might be the right answer, if so, I am looking for help implementing mergeWith in the best possible way as I am not sure how to do it. Any suggestions?
javascript arrays lodash javascript-objects
I have an array of arrays of objects which looks like this:
let fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
I want to reduce and merge the above array using lodash into one so that the samples get concatenated together like so:
fruitSamples = [
{'id': 1, 'type': 'apples', 'samples': [1,2,3,5,2,9,11,2,33]},
{'id': 2, 'type': 'bananas', 'samples': [1,2,7,1,7,7,17,2,67]},
{'id': 3, 'type': 'pears', 'samples': [1,2,3,12,21,32,91,22,34]},
]
I have tried many different approaches but since I want the shortest possible way of solving this what would be your recommendations?
I have tried this:
let test = _(fruitSamples)
.flatten()
.groupBy('type')
.map(_.spread(_.merge))
.value();
console.log(test);
This gives me the following result, which does not concatenate the samples:
test = [
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
I feel using _.mergeWith might be the right answer, if so, I am looking for help implementing mergeWith in the best possible way as I am not sure how to do it. Any suggestions?
javascript arrays lodash javascript-objects
javascript arrays lodash javascript-objects
asked Jan 3 at 15:52
DaybreakDaybreak
82128
82128
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
Instead of _.merge
, you could try using _.mergeWith
that accepts a customizer
function which you can use to customize the assigned values.
From the official docs:
This method is like
_.merge
except that it accepts customizer which is
invoked to produce the merged values of the destination and source
properties.
let fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
function customizer(objValue, srcValue) {
if (_.isArray(objValue)) {
return objValue.concat(srcValue);
}
}
let test = _(fruitSamples)
.flatten()
.groupBy('type')
.map(_.spread((...values) => {
return _.mergeWith(...values, customizer);
}))
.value();
console.log(test);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
Even though I used the no-lodash answer, this helped me understand mergeWith function. Thank you.
– Daybreak
Jan 4 at 11:31
You're welcome.:)
– Ana Liza Pandac
Jan 4 at 12:26
add a comment |
Not sure how to do using lodash, but pure Javascript it's not too difficult.
Update: Thanks to @Taki doing a
flat()
first makes this even
easier..
let fruitSamples = [[{"id":1,"type":"apples","samples":[1,2,3]},{"id":2,"type":"bananas","samples":[1,2,7]},{"id":3,"type":"pears","samples":[1,2,3]}],[{"id":1,"type":"apples","samples":[5,2,9]},{"id":2,"type":"bananas","samples":[1,7,7]},{"id":3,"type":"pears","samples":[12,21,32]}],[{"id":1,"type":"apples","samples":[11,2,33]},{"id":2,"type":"bananas","samples":[17,2,67]},{"id":3,"type":"pears","samples":[91,22,34]}]];
let concat = fruitSamples.flat().reduce((a, i) => {
const f = a.find(f => f.id === i.id);
if (!f) a.push(i);
else f.samples = [...f.samples, ...i.samples];
return a;
}, );
console.log(concat);
2
you could avoid nested loops if you usefruitSamples.flat().reduce()
– Taki
Jan 3 at 16:15
1
@Taki Good idea, I'll update..
– Keith
Jan 3 at 17:09
Works like a charm, although I believe the flat() method does not work in older versions of chrome and some other browsers (I have not tested them all), so I will stick with using forEach for now. Cheers.
– Daybreak
Jan 4 at 11:35
@Daybreak No problem,. Any answers I give I always try and use modern Javascript techniques, I like to go forward not backwards. With polyfill's & transpilers the standard way to create a good web app, there is no reason not too :)
– Keith
Jan 4 at 11:39
add a comment |
You can first convert the array or arrays to simple array using .reduce
and then group by using .reduce
again.
May be more easy to grab
Something like
let fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
var merged = fruitSamples.reduce(function(prev, next) {
return prev.concat(next);
});
var res = merged.reduce(function(prev, next) {
var index = prev.findIndex(o=> o.id == next.id)
if(index >= 0)
prev[index].samples = [...prev[index].samples,...next.samples];
else
prev.push(next);
return prev;
},);
console.log(res)
add a comment |
One way to do it with javascript and no lodash:
var fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
var test = fruitSamples.flat().reduce((rv,x)=>{
var f = rv.find(y => y.id == x.id);
if(f == null){
f ={"id":x.id, "type":x.type,"samples":x.samples};
rv.push(f);
}
else{
f.samples = f.samples.concat(x.samples);
}
return rv;
},);
console.log(JSON.stringify(test))
https://jsfiddle.net/pimboden40/y8x96sbg/1/
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025657%2fhow-to-merge-concatenate-values-of-same-object-properties-in-an-array-of-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Instead of _.merge
, you could try using _.mergeWith
that accepts a customizer
function which you can use to customize the assigned values.
From the official docs:
This method is like
_.merge
except that it accepts customizer which is
invoked to produce the merged values of the destination and source
properties.
let fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
function customizer(objValue, srcValue) {
if (_.isArray(objValue)) {
return objValue.concat(srcValue);
}
}
let test = _(fruitSamples)
.flatten()
.groupBy('type')
.map(_.spread((...values) => {
return _.mergeWith(...values, customizer);
}))
.value();
console.log(test);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
Even though I used the no-lodash answer, this helped me understand mergeWith function. Thank you.
– Daybreak
Jan 4 at 11:31
You're welcome.:)
– Ana Liza Pandac
Jan 4 at 12:26
add a comment |
Instead of _.merge
, you could try using _.mergeWith
that accepts a customizer
function which you can use to customize the assigned values.
From the official docs:
This method is like
_.merge
except that it accepts customizer which is
invoked to produce the merged values of the destination and source
properties.
let fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
function customizer(objValue, srcValue) {
if (_.isArray(objValue)) {
return objValue.concat(srcValue);
}
}
let test = _(fruitSamples)
.flatten()
.groupBy('type')
.map(_.spread((...values) => {
return _.mergeWith(...values, customizer);
}))
.value();
console.log(test);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
Even though I used the no-lodash answer, this helped me understand mergeWith function. Thank you.
– Daybreak
Jan 4 at 11:31
You're welcome.:)
– Ana Liza Pandac
Jan 4 at 12:26
add a comment |
Instead of _.merge
, you could try using _.mergeWith
that accepts a customizer
function which you can use to customize the assigned values.
From the official docs:
This method is like
_.merge
except that it accepts customizer which is
invoked to produce the merged values of the destination and source
properties.
let fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
function customizer(objValue, srcValue) {
if (_.isArray(objValue)) {
return objValue.concat(srcValue);
}
}
let test = _(fruitSamples)
.flatten()
.groupBy('type')
.map(_.spread((...values) => {
return _.mergeWith(...values, customizer);
}))
.value();
console.log(test);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
Instead of _.merge
, you could try using _.mergeWith
that accepts a customizer
function which you can use to customize the assigned values.
From the official docs:
This method is like
_.merge
except that it accepts customizer which is
invoked to produce the merged values of the destination and source
properties.
let fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
function customizer(objValue, srcValue) {
if (_.isArray(objValue)) {
return objValue.concat(srcValue);
}
}
let test = _(fruitSamples)
.flatten()
.groupBy('type')
.map(_.spread((...values) => {
return _.mergeWith(...values, customizer);
}))
.value();
console.log(test);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
let fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
function customizer(objValue, srcValue) {
if (_.isArray(objValue)) {
return objValue.concat(srcValue);
}
}
let test = _(fruitSamples)
.flatten()
.groupBy('type')
.map(_.spread((...values) => {
return _.mergeWith(...values, customizer);
}))
.value();
console.log(test);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
let fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
function customizer(objValue, srcValue) {
if (_.isArray(objValue)) {
return objValue.concat(srcValue);
}
}
let test = _(fruitSamples)
.flatten()
.groupBy('type')
.map(_.spread((...values) => {
return _.mergeWith(...values, customizer);
}))
.value();
console.log(test);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
answered Jan 3 at 16:24
Ana Liza PandacAna Liza Pandac
2,99421324
2,99421324
Even though I used the no-lodash answer, this helped me understand mergeWith function. Thank you.
– Daybreak
Jan 4 at 11:31
You're welcome.:)
– Ana Liza Pandac
Jan 4 at 12:26
add a comment |
Even though I used the no-lodash answer, this helped me understand mergeWith function. Thank you.
– Daybreak
Jan 4 at 11:31
You're welcome.:)
– Ana Liza Pandac
Jan 4 at 12:26
Even though I used the no-lodash answer, this helped me understand mergeWith function. Thank you.
– Daybreak
Jan 4 at 11:31
Even though I used the no-lodash answer, this helped me understand mergeWith function. Thank you.
– Daybreak
Jan 4 at 11:31
You're welcome.:)
– Ana Liza Pandac
Jan 4 at 12:26
You're welcome.:)
– Ana Liza Pandac
Jan 4 at 12:26
add a comment |
Not sure how to do using lodash, but pure Javascript it's not too difficult.
Update: Thanks to @Taki doing a
flat()
first makes this even
easier..
let fruitSamples = [[{"id":1,"type":"apples","samples":[1,2,3]},{"id":2,"type":"bananas","samples":[1,2,7]},{"id":3,"type":"pears","samples":[1,2,3]}],[{"id":1,"type":"apples","samples":[5,2,9]},{"id":2,"type":"bananas","samples":[1,7,7]},{"id":3,"type":"pears","samples":[12,21,32]}],[{"id":1,"type":"apples","samples":[11,2,33]},{"id":2,"type":"bananas","samples":[17,2,67]},{"id":3,"type":"pears","samples":[91,22,34]}]];
let concat = fruitSamples.flat().reduce((a, i) => {
const f = a.find(f => f.id === i.id);
if (!f) a.push(i);
else f.samples = [...f.samples, ...i.samples];
return a;
}, );
console.log(concat);
2
you could avoid nested loops if you usefruitSamples.flat().reduce()
– Taki
Jan 3 at 16:15
1
@Taki Good idea, I'll update..
– Keith
Jan 3 at 17:09
Works like a charm, although I believe the flat() method does not work in older versions of chrome and some other browsers (I have not tested them all), so I will stick with using forEach for now. Cheers.
– Daybreak
Jan 4 at 11:35
@Daybreak No problem,. Any answers I give I always try and use modern Javascript techniques, I like to go forward not backwards. With polyfill's & transpilers the standard way to create a good web app, there is no reason not too :)
– Keith
Jan 4 at 11:39
add a comment |
Not sure how to do using lodash, but pure Javascript it's not too difficult.
Update: Thanks to @Taki doing a
flat()
first makes this even
easier..
let fruitSamples = [[{"id":1,"type":"apples","samples":[1,2,3]},{"id":2,"type":"bananas","samples":[1,2,7]},{"id":3,"type":"pears","samples":[1,2,3]}],[{"id":1,"type":"apples","samples":[5,2,9]},{"id":2,"type":"bananas","samples":[1,7,7]},{"id":3,"type":"pears","samples":[12,21,32]}],[{"id":1,"type":"apples","samples":[11,2,33]},{"id":2,"type":"bananas","samples":[17,2,67]},{"id":3,"type":"pears","samples":[91,22,34]}]];
let concat = fruitSamples.flat().reduce((a, i) => {
const f = a.find(f => f.id === i.id);
if (!f) a.push(i);
else f.samples = [...f.samples, ...i.samples];
return a;
}, );
console.log(concat);
2
you could avoid nested loops if you usefruitSamples.flat().reduce()
– Taki
Jan 3 at 16:15
1
@Taki Good idea, I'll update..
– Keith
Jan 3 at 17:09
Works like a charm, although I believe the flat() method does not work in older versions of chrome and some other browsers (I have not tested them all), so I will stick with using forEach for now. Cheers.
– Daybreak
Jan 4 at 11:35
@Daybreak No problem,. Any answers I give I always try and use modern Javascript techniques, I like to go forward not backwards. With polyfill's & transpilers the standard way to create a good web app, there is no reason not too :)
– Keith
Jan 4 at 11:39
add a comment |
Not sure how to do using lodash, but pure Javascript it's not too difficult.
Update: Thanks to @Taki doing a
flat()
first makes this even
easier..
let fruitSamples = [[{"id":1,"type":"apples","samples":[1,2,3]},{"id":2,"type":"bananas","samples":[1,2,7]},{"id":3,"type":"pears","samples":[1,2,3]}],[{"id":1,"type":"apples","samples":[5,2,9]},{"id":2,"type":"bananas","samples":[1,7,7]},{"id":3,"type":"pears","samples":[12,21,32]}],[{"id":1,"type":"apples","samples":[11,2,33]},{"id":2,"type":"bananas","samples":[17,2,67]},{"id":3,"type":"pears","samples":[91,22,34]}]];
let concat = fruitSamples.flat().reduce((a, i) => {
const f = a.find(f => f.id === i.id);
if (!f) a.push(i);
else f.samples = [...f.samples, ...i.samples];
return a;
}, );
console.log(concat);
Not sure how to do using lodash, but pure Javascript it's not too difficult.
Update: Thanks to @Taki doing a
flat()
first makes this even
easier..
let fruitSamples = [[{"id":1,"type":"apples","samples":[1,2,3]},{"id":2,"type":"bananas","samples":[1,2,7]},{"id":3,"type":"pears","samples":[1,2,3]}],[{"id":1,"type":"apples","samples":[5,2,9]},{"id":2,"type":"bananas","samples":[1,7,7]},{"id":3,"type":"pears","samples":[12,21,32]}],[{"id":1,"type":"apples","samples":[11,2,33]},{"id":2,"type":"bananas","samples":[17,2,67]},{"id":3,"type":"pears","samples":[91,22,34]}]];
let concat = fruitSamples.flat().reduce((a, i) => {
const f = a.find(f => f.id === i.id);
if (!f) a.push(i);
else f.samples = [...f.samples, ...i.samples];
return a;
}, );
console.log(concat);
let fruitSamples = [[{"id":1,"type":"apples","samples":[1,2,3]},{"id":2,"type":"bananas","samples":[1,2,7]},{"id":3,"type":"pears","samples":[1,2,3]}],[{"id":1,"type":"apples","samples":[5,2,9]},{"id":2,"type":"bananas","samples":[1,7,7]},{"id":3,"type":"pears","samples":[12,21,32]}],[{"id":1,"type":"apples","samples":[11,2,33]},{"id":2,"type":"bananas","samples":[17,2,67]},{"id":3,"type":"pears","samples":[91,22,34]}]];
let concat = fruitSamples.flat().reduce((a, i) => {
const f = a.find(f => f.id === i.id);
if (!f) a.push(i);
else f.samples = [...f.samples, ...i.samples];
return a;
}, );
console.log(concat);
let fruitSamples = [[{"id":1,"type":"apples","samples":[1,2,3]},{"id":2,"type":"bananas","samples":[1,2,7]},{"id":3,"type":"pears","samples":[1,2,3]}],[{"id":1,"type":"apples","samples":[5,2,9]},{"id":2,"type":"bananas","samples":[1,7,7]},{"id":3,"type":"pears","samples":[12,21,32]}],[{"id":1,"type":"apples","samples":[11,2,33]},{"id":2,"type":"bananas","samples":[17,2,67]},{"id":3,"type":"pears","samples":[91,22,34]}]];
let concat = fruitSamples.flat().reduce((a, i) => {
const f = a.find(f => f.id === i.id);
if (!f) a.push(i);
else f.samples = [...f.samples, ...i.samples];
return a;
}, );
console.log(concat);
edited Jan 3 at 17:09
answered Jan 3 at 16:04
KeithKeith
9,1281821
9,1281821
2
you could avoid nested loops if you usefruitSamples.flat().reduce()
– Taki
Jan 3 at 16:15
1
@Taki Good idea, I'll update..
– Keith
Jan 3 at 17:09
Works like a charm, although I believe the flat() method does not work in older versions of chrome and some other browsers (I have not tested them all), so I will stick with using forEach for now. Cheers.
– Daybreak
Jan 4 at 11:35
@Daybreak No problem,. Any answers I give I always try and use modern Javascript techniques, I like to go forward not backwards. With polyfill's & transpilers the standard way to create a good web app, there is no reason not too :)
– Keith
Jan 4 at 11:39
add a comment |
2
you could avoid nested loops if you usefruitSamples.flat().reduce()
– Taki
Jan 3 at 16:15
1
@Taki Good idea, I'll update..
– Keith
Jan 3 at 17:09
Works like a charm, although I believe the flat() method does not work in older versions of chrome and some other browsers (I have not tested them all), so I will stick with using forEach for now. Cheers.
– Daybreak
Jan 4 at 11:35
@Daybreak No problem,. Any answers I give I always try and use modern Javascript techniques, I like to go forward not backwards. With polyfill's & transpilers the standard way to create a good web app, there is no reason not too :)
– Keith
Jan 4 at 11:39
2
2
you could avoid nested loops if you use
fruitSamples.flat().reduce()
– Taki
Jan 3 at 16:15
you could avoid nested loops if you use
fruitSamples.flat().reduce()
– Taki
Jan 3 at 16:15
1
1
@Taki Good idea, I'll update..
– Keith
Jan 3 at 17:09
@Taki Good idea, I'll update..
– Keith
Jan 3 at 17:09
Works like a charm, although I believe the flat() method does not work in older versions of chrome and some other browsers (I have not tested them all), so I will stick with using forEach for now. Cheers.
– Daybreak
Jan 4 at 11:35
Works like a charm, although I believe the flat() method does not work in older versions of chrome and some other browsers (I have not tested them all), so I will stick with using forEach for now. Cheers.
– Daybreak
Jan 4 at 11:35
@Daybreak No problem,. Any answers I give I always try and use modern Javascript techniques, I like to go forward not backwards. With polyfill's & transpilers the standard way to create a good web app, there is no reason not too :)
– Keith
Jan 4 at 11:39
@Daybreak No problem,. Any answers I give I always try and use modern Javascript techniques, I like to go forward not backwards. With polyfill's & transpilers the standard way to create a good web app, there is no reason not too :)
– Keith
Jan 4 at 11:39
add a comment |
You can first convert the array or arrays to simple array using .reduce
and then group by using .reduce
again.
May be more easy to grab
Something like
let fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
var merged = fruitSamples.reduce(function(prev, next) {
return prev.concat(next);
});
var res = merged.reduce(function(prev, next) {
var index = prev.findIndex(o=> o.id == next.id)
if(index >= 0)
prev[index].samples = [...prev[index].samples,...next.samples];
else
prev.push(next);
return prev;
},);
console.log(res)
add a comment |
You can first convert the array or arrays to simple array using .reduce
and then group by using .reduce
again.
May be more easy to grab
Something like
let fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
var merged = fruitSamples.reduce(function(prev, next) {
return prev.concat(next);
});
var res = merged.reduce(function(prev, next) {
var index = prev.findIndex(o=> o.id == next.id)
if(index >= 0)
prev[index].samples = [...prev[index].samples,...next.samples];
else
prev.push(next);
return prev;
},);
console.log(res)
add a comment |
You can first convert the array or arrays to simple array using .reduce
and then group by using .reduce
again.
May be more easy to grab
Something like
let fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
var merged = fruitSamples.reduce(function(prev, next) {
return prev.concat(next);
});
var res = merged.reduce(function(prev, next) {
var index = prev.findIndex(o=> o.id == next.id)
if(index >= 0)
prev[index].samples = [...prev[index].samples,...next.samples];
else
prev.push(next);
return prev;
},);
console.log(res)
You can first convert the array or arrays to simple array using .reduce
and then group by using .reduce
again.
May be more easy to grab
Something like
let fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
var merged = fruitSamples.reduce(function(prev, next) {
return prev.concat(next);
});
var res = merged.reduce(function(prev, next) {
var index = prev.findIndex(o=> o.id == next.id)
if(index >= 0)
prev[index].samples = [...prev[index].samples,...next.samples];
else
prev.push(next);
return prev;
},);
console.log(res)
let fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
var merged = fruitSamples.reduce(function(prev, next) {
return prev.concat(next);
});
var res = merged.reduce(function(prev, next) {
var index = prev.findIndex(o=> o.id == next.id)
if(index >= 0)
prev[index].samples = [...prev[index].samples,...next.samples];
else
prev.push(next);
return prev;
},);
console.log(res)
let fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
var merged = fruitSamples.reduce(function(prev, next) {
return prev.concat(next);
});
var res = merged.reduce(function(prev, next) {
var index = prev.findIndex(o=> o.id == next.id)
if(index >= 0)
prev[index].samples = [...prev[index].samples,...next.samples];
else
prev.push(next);
return prev;
},);
console.log(res)
answered Jan 3 at 16:18
George BaileyGeorge Bailey
8,1151130
8,1151130
add a comment |
add a comment |
One way to do it with javascript and no lodash:
var fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
var test = fruitSamples.flat().reduce((rv,x)=>{
var f = rv.find(y => y.id == x.id);
if(f == null){
f ={"id":x.id, "type":x.type,"samples":x.samples};
rv.push(f);
}
else{
f.samples = f.samples.concat(x.samples);
}
return rv;
},);
console.log(JSON.stringify(test))
https://jsfiddle.net/pimboden40/y8x96sbg/1/
add a comment |
One way to do it with javascript and no lodash:
var fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
var test = fruitSamples.flat().reduce((rv,x)=>{
var f = rv.find(y => y.id == x.id);
if(f == null){
f ={"id":x.id, "type":x.type,"samples":x.samples};
rv.push(f);
}
else{
f.samples = f.samples.concat(x.samples);
}
return rv;
},);
console.log(JSON.stringify(test))
https://jsfiddle.net/pimboden40/y8x96sbg/1/
add a comment |
One way to do it with javascript and no lodash:
var fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
var test = fruitSamples.flat().reduce((rv,x)=>{
var f = rv.find(y => y.id == x.id);
if(f == null){
f ={"id":x.id, "type":x.type,"samples":x.samples};
rv.push(f);
}
else{
f.samples = f.samples.concat(x.samples);
}
return rv;
},);
console.log(JSON.stringify(test))
https://jsfiddle.net/pimboden40/y8x96sbg/1/
One way to do it with javascript and no lodash:
var fruitSamples = [
[
{'id': 1,'type': 'apples','samples': [1, 2, 3]},
{'id': 2,'type': 'bananas','samples': [1, 2, 7]},
{'id': 3,'type': 'pears','samples': [1, 2, 3]}
],
[
{'id': 1,'type': 'apples','samples': [5, 2, 9]},
{'id': 2,'type': 'bananas','samples': [1, 7, 7]},
{'id': 3,'type': 'pears','samples': [12, 21, 32]}
],
[
{'id': 1,'type': 'apples','samples': [11, 2, 33]},
{'id': 2,'type': 'bananas','samples': [17, 2, 67]},
{'id': 3,'type': 'pears','samples': [91, 22, 34]}
]
];
var test = fruitSamples.flat().reduce((rv,x)=>{
var f = rv.find(y => y.id == x.id);
if(f == null){
f ={"id":x.id, "type":x.type,"samples":x.samples};
rv.push(f);
}
else{
f.samples = f.samples.concat(x.samples);
}
return rv;
},);
console.log(JSON.stringify(test))
https://jsfiddle.net/pimboden40/y8x96sbg/1/
answered Jan 3 at 16:55
CodeHackerCodeHacker
1,3991428
1,3991428
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025657%2fhow-to-merge-concatenate-values-of-same-object-properties-in-an-array-of-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown