How to subtract one array from another, in javascript [duplicate] - javascript

This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 2 years ago.
If I have an array A = [1, 2, 3, 4, 5] and B = [3, 4, 5] I want to return a new array with values
[1, 2]. remove same value.

Using Array.prototype.includes, you can check if B contains A item or not.
And using Array.prototype.filter, you can get the filtered values which are not included in array B.
const A = [1, 2, 3, 4, 5];
const B = [3, 4, 5];
const output = A.filter((item) => !B.includes(item));
console.log(output);

Related

How to remove an array from a nested array in JavaScript? [duplicate]

This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
Closed 1 year ago.
Is there a way to remove an array from a nested array in JavaScript?
I have the following array:
arr = [
[1, 2],
[2, 3],
[3, 4]
]
and I want to remove the value [2, 3] from the array so that it results with:
arr = [
[1, 2],
[3, 4]
]
I have already tried the answers on How can I remove a specific item from an array?, but they don't seem to work. I was wondering if there was a fast efficient way of doing this.
Edit:
I already tried using indexOf and findIndex, and it does not return the index of an array inside of the array.
arr = [
[1, 2],
[2, 3],
[3, 4]
];
console.log(arr.indexOf([2, 3]));
console.log(arr.findIndex([2, 3]));
This did not work, even though it was suggested in the comments below.
Furthermore, using:
console.log(arr.filter(nested => nested[0] !== 2 || nested[1] !== 3));
will be inefficient as in my code I need to remove large lists, which have hundreds of values, I only provided an example in my question.
Any help would be appreciated.
var arr = [
[1, 2],
[2, 3],
[3, 4]
]
console.log('Before =>', arr);
arr.splice(1, 1); // .splice(index, 1);
console.log('After=>', arr);

Remove duplicate elements and take one element from two array [duplicate]

This question already has answers here:
Remove duplicate values from JS array [duplicate]
(54 answers)
How to merge two arrays in JavaScript and de-duplicate items
(89 answers)
Closed 2 years ago.
I have two arrays below,
let array_one = [1, 2, 3, 4, 5]; //first array
let array_two = [5, 6, 7, 8, 9]; //second array
//I need [1, 2, 3, 4, 5, 6, 7, 8, 9]
In array_one and array_two has one common element that is 5 but I need this 5 only one time. I don't need to remove the duplicate elements but need one single element.
Please try this, It should work for you
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
let arr = [...array_one,...array_two];
arr.filter(onlyUnique);

Filter an array returns empty

I'm trying to populate an array with all values retrieved (I am in a loop) and then to delete values already present in the yearsJustSelected array:
yearsJustSelected.forEach((el:any)=>{
yearsJustSelectedAll.push(el);
});
yearsJustSelectedFiltered = yearsJustSelected.filter((el: any) => !yearsJustSelectedAll.includes(el));
For example:
loop 1 --> yearsJustSelected=[1, 2, 3] - yearsJustSelectedAll=[1, 2, 3] - yearsJustSelectedFiltered = []
loop 2 --> yearsJustSelected=[2, 3, 4, 5] - yearsJustSelectedAll=[1, 2, 3, 4, 5] - yearsJustSelectedFiltered=[4, 5] --> because [2, 3 ] were already present in the yearsJustSelectedAll in the first loop
This code:
yearsJustSelectedFiltered = yearsJustSelected.filter((el: any) => !yearsJustSelectedAll.includes(el));
always return an empty array.
yearsJustSelected.forEach((el:any)=>{
yearsJustSelectedAll.push(el);
});
// The code above will add all elements in yearsJustSelected into yearsJustSelectedAll
// making the two arrays the same.
There are no instances where one array has elements that are not in the other array
You could check if the target array does not have the item. Then push the item to the target array.
function select(source, target) {
source.forEach(v => target.includes(v) || target.push(v));
}
var yearsJustSelectedAll = [];
select([1, 2, 3], yearsJustSelectedAll);
console.log(...yearsJustSelectedAll);
select([2, 3, 4, 5], yearsJustSelectedAll);
console.log(...yearsJustSelectedAll);
select([4, 5], yearsJustSelectedAll);
console.log(...yearsJustSelectedAll);

JS Array Overwritten in Function [duplicate]

This question already has answers here:
Copy array by value
(39 answers)
Closed 6 years ago.
I'm working on a larger project, and I've encountered trouble with arrays, demonstrated below.
var x = new Array();
x = [5, 2];
function doStuff(a){
a[0]++;
console.log(a);//Prints [6, 2]
}
doStuff(x);
console.log(x);//Prints [6, 2] when it should print [5, 2]
How could I do things with an array passed to a function without modifying the original?
What you're passing to doStuff is a reference to the array. You're not actually passing the data.
You're going to have to explicitly copy the array, in order not to modify the source:
var x = [5, 2]; // No need to use the `Array` constructor.
function doStuff(a) {
var x = a.slice(0); // Copy the array.
x[0]++;
console.log(x); // Prints [6, 2]
}
doStuff(x);
console.log(x); // Prints [5, 2]

.splice() - how to protect oryginal array? [duplicate]

This question already has answers here:
Why does changing an Array in JavaScript affect copies of the array?
(12 answers)
Closed 7 years ago.
var a = [1, 2, 3];
var b = a; // b = [1, 2, 3];
console.log(a); // 1, 2, 3
console.log(b); // 1, 2, 3
b.splice(0, 1);
console.log(b); // 2, 3
console.log(a); // 2, 3 <----------- WHY?
I just needed to copy my oryginal "a" array because I want to stay it as [1, 2, 3] forever. How to pop first element from "b" array without touching the oryginal one? Thanks
Your code just needs one small fix:
var a = [1, 2, 3];
var b = a.slice();
I'm not sure of the specifics, but when you are assigning arrays or objects to another variable, the array/object is not copied by value, but rather by reference. .slice method duplicates all elements in the array to the new element rather than just saving a reference to the old one.

Categories