How to get union of several immutable js Lists

0 votes

I have List a:

let a = Immutable.List([1])

and List b:

let b = Immutable.List([2, 3])

I want to get List union === List([1, 2, 3]) from them.

I try to merge them fist:

let union = a.merge(b); // List([2, 3])

It seems like merge method operates with indexes, not with values so overrides first item of List a with first item of List b. So, my question is what is the most simple way to get union of several lists.

Nov 30, 2020 in Node-js by kartik
• 37,520 points
893 views

1 answer to this question.

0 votes

Hello @kartik,

Merge will update the index with the current value of the merging list. So in your case you had

[0] = 1

and merged it with

[0] = 2
[1] = 3

which ended up overwriting [0]=1 with [0]=2, and then set [1]=3 resulting in your observed [2,3] array after merging.

A very simple approach to solving this would be to use concat

var a = Immutable.List([1]);
var b = Immutable.List([2,3]); 

var c = a.concat(b);

And it will work for this situation. However, if the situation is more complex, this may be incorrect. For example,

var a = Immutable.List([1,4]);
var b = Immutable.List([2,3,4]); 
answered Nov 30, 2020 by Niroj
• 82,840 points

Related Questions In Node-js

0 votes
1 answer

How do I get the path to the current script with Node.js?

Hello @kartik, you can do this: fs.readFile(path.resolve(__dirname, 'settings.json'), 'UTF-8', ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,840 points
2,828 views
0 votes
1 answer

How can i get the extension of the image in node.js?

Hello @kar You can do the following to ...READ MORE

answered Jul 16, 2020 in Node-js by Niroj
• 82,840 points
2,090 views
0 votes
1 answer

How to get all count of mongoose model?

Hello @kartik, The reason your code doesn't work ...READ MORE

answered Sep 7, 2020 in Node-js by Niroj
• 82,840 points
4,450 views
0 votes
1 answer

How do I get the time of day in javascript/Node.js?

Hello @kartik, This function will return you the ...READ MORE

answered Sep 7, 2020 in Node-js by Niroj
• 82,840 points
1,171 views
+1 vote
1 answer

What is the relationship between angularjs Scope with controller/view?

Let us consider the below block: <div ng-controller="emp"> ...READ MORE

answered Jan 20, 2020 in Web Development by Niroj
• 82,840 points

edited Jan 21, 2020 by Niroj 1,144 views
0 votes
1 answer

How can we avoid my php form from hacking?

Hii @kartik, If you want to know php ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,840 points
2,593 views
0 votes
1 answer

What is a Cookie? How to create Cookies With PHP?

A cookie is often used to identify ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,840 points
3,792 views
0 votes
1 answer

How to shrink Navigation menu or bar on Scroll?

Hey, You can follow the steps below in ...READ MORE

answered Feb 19, 2020 in PHP by varun
3,780 views
0 votes
1 answer

How to get data out of a Node.js http get request?

Hello @kartik, Your logs return undefined : you log before ...READ MORE

answered Oct 12, 2020 in Node-js by Niroj
• 82,840 points
6,987 views
0 votes
1 answer

How to get GET (query string) variables in Express.js on Node.js?

Hello @kartik, Since you've mentioned Express.js in your ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,840 points
3,257 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP