This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Remove item from array by value | JavaScript
Is there a way, in JS or with help of jQuery, to remove element from an array with knowing just the value of the element, not it's place inside an array?
It needs to be cross-browser compatible too.
Thanks.
look to the example
arr = [1, 2, 3, 4, 5] // array
var removeItem = 2; // item to be removed
arr = jQuery.grep(arr, function(value) {
return value != removeItem;
});
// new array
// [1, 3, 4, 5]
Related
This question already has an answer here:
Get all elements in an array after a certain index in JavaScript
(1 answer)
Closed last month.
For example I have an array like [1, 2, 3, 4, 5] , and I want to select [3, 4, 5].
Is there a simple way to do that? Lots of thanks.
You can use slicing for this.
Lets say x = [1,2,3,4,5]. Just do s.slice(2).
You can just use a simple for loop and begin looping at the nth element of the array.
let array = [1, 2, 3, 4, 5]
for (let i = 2; i < array.length; i++) {
console.log(array[i])
}
This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 1 year ago.
This is what I have
function array_diff_very_fast(a, b) {
a = a.filter( el => !b.includes( el ) );
return a;
}
var temp = array_diff_very_fast([1,2,2,2,2,4,5,6], [1,2,6]);
console.log(temp);
and I want it to return [4,5].
I am working in code wars and the function works but it is not efficient enough.
You could take an approach with O(1) for searching the element in a data set, like a Set or an object.
Then omit using a variable just for storing a return value.
function array_diff_very_fast(a, b) {
const bb = new Set(b);
return a.filter(el => !bb.has(el));
}
console.log(array_diff_very_fast([1, 2, 2, 2, 2, 4, 5, 6], [1, 2, 6]));
This question already has answers here:
Why does a string index in an array not increase the 'length'?
(7 answers)
Are Javascript arrays primitives? Strings? Objects?
(7 answers)
Closed 3 years ago.
A strange behavior of array assignment at index: array[<some string>].
Let's say I have a normal array:
const arr = [1, 2, 3]; // undefined
If I type:
arr['.'] = 4; // 4
arr // (3) [1, 2, 3, .: 4]
arr.length // 3
for (a of arr) {
console.log(a)
// 1
// 2
// 3
}
Now I get some strange hybrid between an array and an object.
If I try to spread the array into another variable:
const barr = [...arr] // (3) [1, 2, 3]
Does anyone know what is going on and what rules create this kind of behavior?
This question already has answers here:
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
(97 answers)
Closed 6 years ago.
There is a javascript array
var arr = [0, 1, 2, 2, 3, 3, 5];
I want to choose elements that repeats twice. In this case its 2 and 3. and i want attach them into a variable.
var a = 2, b = 3;
As far as i know there is no built-in function to do that job. How can i do that. Thanks.
You can use filter to get the values that occur twice.
var arr = [0, 1, 2, 2, 3, 3, 5];
var dups = arr.filter ( (v,i,a) => a.indexOf(v) < i );
console.log(dups);
In comments you stated you would only have doubles, but no values that occur more than twice. Note that the above would return a value more than once, if the latter would be the case.
This returns the values in an array, which is how you should work. To put them in separate values can be done as follows:
var [a, b, ...others] = dups;
...but you would have to know how many variables to reserve for that, and it does not make your further program any easier. JavaScript has many nice functions (methods) for arrays, so you should in fact leave them in an array.
There is no built in function to do that indeed.
You will have to loop thought the array and keeping track of the number of occurrences of the elements, while building a response array.
You could filter a sorted array.
var arr = [0, 1, 2, 2, 3, 3, 5],
repeats = arr.filter(function (a, i, aa) {
return aa[i - 1] === a;
});
console.log(repeats);
Most simple way to do this is the following:
var dups = [];
var arr = [0, 1, 2, 2, 3, 3, 5];
arr.forEach(function (v, i, a){
delete arr[i];
if (arr.indexOf(v) !== -1){
dups.push(v);
}
});
console.log(dups);
It's destructive however.
This question already has answers here:
Simplest code for array intersection in javascript
(40 answers)
Closed 8 years ago.
I don't want to compare the two arrays as a whole, I specifically want to check if the 2nd array has any values that match a value in the first one. I then want to return the value that matches in both.
If I'm not mistaken, comparing two arrays as a whole would be done like this:
array1 = [1,2,3];
array2 = [1,3,4];
console.log(JSON.encode(array1)==JSON.encode(array2));
So in this case, I would want to check if array2 has any matching values to array one, not if they both are equivalent. Any help is appreciated!
var array1 = [1, 2, 3],
array2 = [1, 3, 4];
var AnyItemsOfArray1InArray2 = array1.some(function(item) {
return array2.indexOf(item) > -1;
});
console.log(AnyItemsOfArray1InArray2);
var ItemsOfArray1InArray2 = array1.filter(function(item) {
return array2.indexOf(item) > -1;
});
console.log(ItemsOfArray1InArray2);