Merging multiple arrays of objects of the same length in JavaScript The Next CEO of Stack OverflowLength of a JavaScript objectWhat is the most efficient way to deep clone an object in JavaScript?How can I merge properties of two JavaScript objects dynamically?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How do I correctly clone a JavaScript object?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
Would this house-rule that treats advantage as a +1 to the roll instead (and disadvantage as -1) and allows them to stack be balanced?
I believe this to be a fraud - hired, then asked to cash check and send cash as Bitcoin
How can I quit an app using Terminal?
Why didn't Khan get resurrected in the Genesis Explosion?
The King's new dress
Why doesn't a table tennis ball float on the surface? How do we calculate buoyancy here?
How to use tikz in fbox?
Why here is plural "We went to the movies last night."
How do I go from 300 unfinished/half written blog posts, to published posts?
Should I tutor a student who I know has cheated on their homework?
WOW air has ceased operation, can I get my tickets refunded?
Why do remote companies require working in the US?
Is the concept of a "numerable" fiber bundle really useful or an empty generalization?
What is the point of a new vote on May's deal when the indicative votes suggest she will not win?
What does this shorthand mean?
How can I get through very long and very dry, but also very useful technical documents when learning a new tool?
Text adventure game code
When airplanes disconnect from a tanker during air to air refueling, why do they bank so sharply to the right?
Only print output after finding pattern
If the heap is initialized for security, then why is the stack uninitialized?
Why do professional authors make "consistency" mistakes? And how to avoid them?
How to write the block matrix in LaTex?
How do scammers retract money, while you can’t?
How to make a variable always equal to the result of some calculations?
Merging multiple arrays of objects of the same length in JavaScript
The Next CEO of Stack OverflowLength of a JavaScript objectWhat is the most efficient way to deep clone an object in JavaScript?How can I merge properties of two JavaScript objects dynamically?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How do I correctly clone a JavaScript object?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
Here are examples of the arrays that I need to merge
const urls = [url:"http:/...",url:"http:/...",url:"http:/..."];
const images = [image:"...",image:"...",image:"..."];
const merged = [url:"http:/...", image:"...",url:"http:/...", image:"...",url:"http:/...", image:"..."]
I have tried Object.assign(,urls, images). That ends up just deleting urls in that instance because it does not deep copy. The keys for each object in the array are the same!
javascript ecmascript-6
add a comment |
Here are examples of the arrays that I need to merge
const urls = [url:"http:/...",url:"http:/...",url:"http:/..."];
const images = [image:"...",image:"...",image:"..."];
const merged = [url:"http:/...", image:"...",url:"http:/...", image:"...",url:"http:/...", image:"..."]
I have tried Object.assign(,urls, images). That ends up just deleting urls in that instance because it does not deep copy. The keys for each object in the array are the same!
javascript ecmascript-6
you can use manual merging - use loopfor()and create new objects... simple I think
– demo
Mar 8 at 12:50
add a comment |
Here are examples of the arrays that I need to merge
const urls = [url:"http:/...",url:"http:/...",url:"http:/..."];
const images = [image:"...",image:"...",image:"..."];
const merged = [url:"http:/...", image:"...",url:"http:/...", image:"...",url:"http:/...", image:"..."]
I have tried Object.assign(,urls, images). That ends up just deleting urls in that instance because it does not deep copy. The keys for each object in the array are the same!
javascript ecmascript-6
Here are examples of the arrays that I need to merge
const urls = [url:"http:/...",url:"http:/...",url:"http:/..."];
const images = [image:"...",image:"...",image:"..."];
const merged = [url:"http:/...", image:"...",url:"http:/...", image:"...",url:"http:/...", image:"..."]
I have tried Object.assign(,urls, images). That ends up just deleting urls in that instance because it does not deep copy. The keys for each object in the array are the same!
javascript ecmascript-6
javascript ecmascript-6
edited Mar 20 at 15:30
Dream Hunter - hashADH
2,14511635
2,14511635
asked Mar 8 at 12:48
AlexMcGAlexMcG
801212
801212
you can use manual merging - use loopfor()and create new objects... simple I think
– demo
Mar 8 at 12:50
add a comment |
you can use manual merging - use loopfor()and create new objects... simple I think
– demo
Mar 8 at 12:50
you can use manual merging - use loop
for() and create new objects... simple I think– demo
Mar 8 at 12:50
you can use manual merging - use loop
for() and create new objects... simple I think– demo
Mar 8 at 12:50
add a comment |
4 Answers
4
active
oldest
votes
If you are certainly sure they are both equal size, you definitely can run through one of them with Array.prototype.map method.
Actually, you should use Object.assign if you want to merge an object with a more generic way.
const urls = [url:"http:/...",url:"http:/...",url:"http:/..."];
const images = [image:"...",image:"...",image:"..."];
const results = urls.map((url, index) =>
Object.assign(, url, images[index])
);
console.log(results)ES6
you can use ES6 instead of Object.assign.
const results = urls.map((url, index) =>
(...url, ...images[index])
);
add a comment |
You could map over one of the arrays and use the index to get the other array's item:
const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."];
const images = [image:"0...",image:"1...",image:"2..."];
const newArray = urls.map((url, i) => (...url, ...images[i] ) )
console.log(newArray)Or you could use Array.from like this:
const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."],
images = [image:"0...",image:"1...",image:"2..."];
const length = urls.length
const newArray = Array.from( length , (_, i) => ( ...urls[i], ...images[i] ) )
console.log(newArray)add a comment |
You could iterate over one of the arrays using Array.prototype.map, and using the index provided to the callback function, you can get corresponding value in the other array.
Here's an example:
const urls = [url:"http:/...",url:"http:/...",url:"http:/..."];
const images = [image:"...",image:"...",image:"..."];
console.log(urls.map((t, id) => ( image: images[id].image, url: t.url )));Here the parameters passed into the callback function are t: the item from urls array and id: index of t in urls array.
add a comment |
You could reduce the arrays and assign the object to the object with the same index.
This works for an arbitrary count of arrays.
const
urls = [url:"http:/...", url: "http:/..." , url: "http:/..." ],
images = [ image: "..." , image: "..." , image: "..." ],
result = [urls, images]
.reduce((r, a) => (
a.forEach((o, i) => Object.assign(r[i] = r[i] || , o)),
r
), []);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; add a comment |
Your Answer
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%2f55063572%2fmerging-multiple-arrays-of-objects-of-the-same-length-in-javascript%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
If you are certainly sure they are both equal size, you definitely can run through one of them with Array.prototype.map method.
Actually, you should use Object.assign if you want to merge an object with a more generic way.
const urls = [url:"http:/...",url:"http:/...",url:"http:/..."];
const images = [image:"...",image:"...",image:"..."];
const results = urls.map((url, index) =>
Object.assign(, url, images[index])
);
console.log(results)ES6
you can use ES6 instead of Object.assign.
const results = urls.map((url, index) =>
(...url, ...images[index])
);
add a comment |
If you are certainly sure they are both equal size, you definitely can run through one of them with Array.prototype.map method.
Actually, you should use Object.assign if you want to merge an object with a more generic way.
const urls = [url:"http:/...",url:"http:/...",url:"http:/..."];
const images = [image:"...",image:"...",image:"..."];
const results = urls.map((url, index) =>
Object.assign(, url, images[index])
);
console.log(results)ES6
you can use ES6 instead of Object.assign.
const results = urls.map((url, index) =>
(...url, ...images[index])
);
add a comment |
If you are certainly sure they are both equal size, you definitely can run through one of them with Array.prototype.map method.
Actually, you should use Object.assign if you want to merge an object with a more generic way.
const urls = [url:"http:/...",url:"http:/...",url:"http:/..."];
const images = [image:"...",image:"...",image:"..."];
const results = urls.map((url, index) =>
Object.assign(, url, images[index])
);
console.log(results)ES6
you can use ES6 instead of Object.assign.
const results = urls.map((url, index) =>
(...url, ...images[index])
);
If you are certainly sure they are both equal size, you definitely can run through one of them with Array.prototype.map method.
Actually, you should use Object.assign if you want to merge an object with a more generic way.
const urls = [url:"http:/...",url:"http:/...",url:"http:/..."];
const images = [image:"...",image:"...",image:"..."];
const results = urls.map((url, index) =>
Object.assign(, url, images[index])
);
console.log(results)ES6
you can use ES6 instead of Object.assign.
const results = urls.map((url, index) =>
(...url, ...images[index])
);
const urls = [url:"http:/...",url:"http:/...",url:"http:/..."];
const images = [image:"...",image:"...",image:"..."];
const results = urls.map((url, index) =>
Object.assign(, url, images[index])
);
console.log(results)const urls = [url:"http:/...",url:"http:/...",url:"http:/..."];
const images = [image:"...",image:"...",image:"..."];
const results = urls.map((url, index) =>
Object.assign(, url, images[index])
);
console.log(results)edited Mar 8 at 13:11
answered Mar 8 at 12:53
Lidor AvitanLidor Avitan
721415
721415
add a comment |
add a comment |
You could map over one of the arrays and use the index to get the other array's item:
const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."];
const images = [image:"0...",image:"1...",image:"2..."];
const newArray = urls.map((url, i) => (...url, ...images[i] ) )
console.log(newArray)Or you could use Array.from like this:
const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."],
images = [image:"0...",image:"1...",image:"2..."];
const length = urls.length
const newArray = Array.from( length , (_, i) => ( ...urls[i], ...images[i] ) )
console.log(newArray)add a comment |
You could map over one of the arrays and use the index to get the other array's item:
const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."];
const images = [image:"0...",image:"1...",image:"2..."];
const newArray = urls.map((url, i) => (...url, ...images[i] ) )
console.log(newArray)Or you could use Array.from like this:
const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."],
images = [image:"0...",image:"1...",image:"2..."];
const length = urls.length
const newArray = Array.from( length , (_, i) => ( ...urls[i], ...images[i] ) )
console.log(newArray)add a comment |
You could map over one of the arrays and use the index to get the other array's item:
const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."];
const images = [image:"0...",image:"1...",image:"2..."];
const newArray = urls.map((url, i) => (...url, ...images[i] ) )
console.log(newArray)Or you could use Array.from like this:
const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."],
images = [image:"0...",image:"1...",image:"2..."];
const length = urls.length
const newArray = Array.from( length , (_, i) => ( ...urls[i], ...images[i] ) )
console.log(newArray)You could map over one of the arrays and use the index to get the other array's item:
const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."];
const images = [image:"0...",image:"1...",image:"2..."];
const newArray = urls.map((url, i) => (...url, ...images[i] ) )
console.log(newArray)Or you could use Array.from like this:
const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."],
images = [image:"0...",image:"1...",image:"2..."];
const length = urls.length
const newArray = Array.from( length , (_, i) => ( ...urls[i], ...images[i] ) )
console.log(newArray)const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."];
const images = [image:"0...",image:"1...",image:"2..."];
const newArray = urls.map((url, i) => (...url, ...images[i] ) )
console.log(newArray)const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."];
const images = [image:"0...",image:"1...",image:"2..."];
const newArray = urls.map((url, i) => (...url, ...images[i] ) )
console.log(newArray)const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."],
images = [image:"0...",image:"1...",image:"2..."];
const length = urls.length
const newArray = Array.from( length , (_, i) => ( ...urls[i], ...images[i] ) )
console.log(newArray)const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."],
images = [image:"0...",image:"1...",image:"2..."];
const length = urls.length
const newArray = Array.from( length , (_, i) => ( ...urls[i], ...images[i] ) )
console.log(newArray)edited Mar 8 at 13:26
answered Mar 8 at 12:52
adigaadiga
12k62545
12k62545
add a comment |
add a comment |
You could iterate over one of the arrays using Array.prototype.map, and using the index provided to the callback function, you can get corresponding value in the other array.
Here's an example:
const urls = [url:"http:/...",url:"http:/...",url:"http:/..."];
const images = [image:"...",image:"...",image:"..."];
console.log(urls.map((t, id) => ( image: images[id].image, url: t.url )));Here the parameters passed into the callback function are t: the item from urls array and id: index of t in urls array.
add a comment |
You could iterate over one of the arrays using Array.prototype.map, and using the index provided to the callback function, you can get corresponding value in the other array.
Here's an example:
const urls = [url:"http:/...",url:"http:/...",url:"http:/..."];
const images = [image:"...",image:"...",image:"..."];
console.log(urls.map((t, id) => ( image: images[id].image, url: t.url )));Here the parameters passed into the callback function are t: the item from urls array and id: index of t in urls array.
add a comment |
You could iterate over one of the arrays using Array.prototype.map, and using the index provided to the callback function, you can get corresponding value in the other array.
Here's an example:
const urls = [url:"http:/...",url:"http:/...",url:"http:/..."];
const images = [image:"...",image:"...",image:"..."];
console.log(urls.map((t, id) => ( image: images[id].image, url: t.url )));Here the parameters passed into the callback function are t: the item from urls array and id: index of t in urls array.
You could iterate over one of the arrays using Array.prototype.map, and using the index provided to the callback function, you can get corresponding value in the other array.
Here's an example:
const urls = [url:"http:/...",url:"http:/...",url:"http:/..."];
const images = [image:"...",image:"...",image:"..."];
console.log(urls.map((t, id) => ( image: images[id].image, url: t.url )));Here the parameters passed into the callback function are t: the item from urls array and id: index of t in urls array.
const urls = [url:"http:/...",url:"http:/...",url:"http:/..."];
const images = [image:"...",image:"...",image:"..."];
console.log(urls.map((t, id) => ( image: images[id].image, url: t.url )));const urls = [url:"http:/...",url:"http:/...",url:"http:/..."];
const images = [image:"...",image:"...",image:"..."];
console.log(urls.map((t, id) => ( image: images[id].image, url: t.url )));answered Mar 8 at 12:51
NisargNisarg
11.2k52341
11.2k52341
add a comment |
add a comment |
You could reduce the arrays and assign the object to the object with the same index.
This works for an arbitrary count of arrays.
const
urls = [url:"http:/...", url: "http:/..." , url: "http:/..." ],
images = [ image: "..." , image: "..." , image: "..." ],
result = [urls, images]
.reduce((r, a) => (
a.forEach((o, i) => Object.assign(r[i] = r[i] || , o)),
r
), []);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; add a comment |
You could reduce the arrays and assign the object to the object with the same index.
This works for an arbitrary count of arrays.
const
urls = [url:"http:/...", url: "http:/..." , url: "http:/..." ],
images = [ image: "..." , image: "..." , image: "..." ],
result = [urls, images]
.reduce((r, a) => (
a.forEach((o, i) => Object.assign(r[i] = r[i] || , o)),
r
), []);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; add a comment |
You could reduce the arrays and assign the object to the object with the same index.
This works for an arbitrary count of arrays.
const
urls = [url:"http:/...", url: "http:/..." , url: "http:/..." ],
images = [ image: "..." , image: "..." , image: "..." ],
result = [urls, images]
.reduce((r, a) => (
a.forEach((o, i) => Object.assign(r[i] = r[i] || , o)),
r
), []);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; You could reduce the arrays and assign the object to the object with the same index.
This works for an arbitrary count of arrays.
const
urls = [url:"http:/...", url: "http:/..." , url: "http:/..." ],
images = [ image: "..." , image: "..." , image: "..." ],
result = [urls, images]
.reduce((r, a) => (
a.forEach((o, i) => Object.assign(r[i] = r[i] || , o)),
r
), []);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; const
urls = [url:"http:/...", url: "http:/..." , url: "http:/..." ],
images = [ image: "..." , image: "..." , image: "..." ],
result = [urls, images]
.reduce((r, a) => (
a.forEach((o, i) => Object.assign(r[i] = r[i] || , o)),
r
), []);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; const
urls = [url:"http:/...", url: "http:/..." , url: "http:/..." ],
images = [ image: "..." , image: "..." , image: "..." ],
result = [urls, images]
.reduce((r, a) => (
a.forEach((o, i) => Object.assign(r[i] = r[i] || , o)),
r
), []);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; answered Mar 8 at 12:59
Nina ScholzNina Scholz
194k15107178
194k15107178
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%2f55063572%2fmerging-multiple-arrays-of-objects-of-the-same-length-in-javascript%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
you can use manual merging - use loop
for()and create new objects... simple I think– demo
Mar 8 at 12:50