This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(141 answers)
Closed 7 years ago.
I have an array in javascript.Example
var array=[1,2,3,4,5,2,6,7,2];
Ok, i want to delete value 2 in array. The result as
var array=[1,3,4,5,6,7];
Thank guys.
Use filter() for that
var array = [1, 2, 3, 4, 5, 2, 6, 7, 2];
array = array.filter(function(v) {
return v != 2
});
console.log(array);
Related
This question already has answers here:
How do I empty an array in JavaScript?
(17 answers)
Closed 2 years ago.
How can I delete all the items from a JavaScript array?
example -
var myArray1, 2, 3, 4, 5, 6, 7, 8, 9];
// I want it to become this
myArray = [];
You can try this:
function clearArray(array) {
while (array.length) {
array.pop();
}
}
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);
This question already has answers here:
How to check if an array contains another array?
(6 answers)
javascript search array of arrays
(12 answers)
How to compare arrays in JavaScript?
(55 answers)
Closed 3 years ago.
These codes don't work:
const a = [[1, 1], [2, 2]]
console.log(a.includes([1, 1])); // --> false
console.log(a.indexOf([1, 1])); // --> -1
This work but I think its not optimized
console.log(a.map(x => x.toString()).includes([1, 1].toString()));
// --> true
Is there a simpler way ?
const a = [[1, 1], [2, 2]]
var index=a.findIndex(x=>{return JSON.stringify(x)===JSON.stringify([2, 2])})
console.log(`item index : ${index}`);
Assuming this:
var arr = ['a', 'b', 'b'];
you can invoke:
Array.isArray(arr);
will return true if the considered variable is an array, otherwise not.
Once you get it, you can apply it to the external array.
This question already has answers here:
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
(97 answers)
Closed 4 years ago.
I have an array, and I want an output if it contains more than 1 of the same element.
Example:
my_array = [1, 2, 3, 1];
if you want a Boolean output if an element is repeated you can do this:
var arr=[1,1,3,4]
let isDup=false;
arr.map(x=>(arr.indexOf(x)!==arr.lastIndexOf(x))?isDup=true:isDup)
console.log(isDup)
Convert the array to a Set. A Set can only contain unique values. If the Set's size is less than the array's length, there are duplicates:
const hasDuplicates = (arr) => arr.length > new Set(arr).size;
console.log(hasDuplicates([1, 2, 3])); // false
console.log(hasDuplicates([1, 2, 3, 1])); // true
This question already has answers here:
Summing ; delimited values in Javascript
(1 answer)
How to sum elements at the same index in array of arrays into a single array?
(7 answers)
Closed 5 years ago.
I am trying to add together an undetermined amount of arrays, but only add or reduce them item by item.
I cannot figure out how to make this work in ES6 javascript.
const arrayList = [
[1, 2, 3],
[1, 2, 3],
[2, 2, 2]
];
const totalRow = arrayList.map((array) => {
const total = [];
array.map((number, i) => {
total[i] = (total[i] == undefined ? 0 : total[i]) + number;
});
return total;
});
//Output is just 3 more arrays with no change, as it seems
//the total variable resets to an empty array with each iteration.
//what i want is this output:
[4, 6, 8]
What am I doing wrong? I tried reduce method as well but came up empty