0

I have two lists as below. How to merge two lists with unique value (1), and how to exclude the second list items in the first list (2)

private List1: [];
private List2: [];
this.List1 = [1, 2, 3, 4, 5];
this.List2 = [2, 4, 6];

Results

1) result = [1, 2, 3, 4, 5, 6]

2) result = [1, 3, 5]
1

1 Answer 1

1

1) For unique set you can work with Set object and give to it an array. Set will remove duplicates automatically.

2) For excluded array you can use Array#filter and using a condition inside it take those items which are not in the list2.

const list1 = [1, 2, 3, 4, 5];
const list2 = [2, 4, 6];

const unique = [...new Set(list1.concat(list2))];
console.log(unique);

const excluded = list1.filter(item => !list2.includes(item));
console.log(excluded);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.