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
Related
This question already has answers here:
to remove first and last element in array
(14 answers)
Closed 3 years ago.
const myOldArr = [2, 3, 6];
const newArr = myOldArr.pop().shift();
Why does doing this give me a TypeError? Is there a way to do this without a loop? I want the new array to be stored inside a variable so the remaining numbers that don't get removed are stored inside an array of some sort.
.pop() returns the last element form your array and so .shift() won't work,
If you want to keep the myOldArr and create a new one without the first and last element, you can do this by using .slice(1, -1):
const myOldArr = [2, 3, 6];
const myNewArr = myOldArr.slice(1, -1);
console.log(myNewArr);
Or, if you don't mind moifying the original array, you can do this by not chaining both methods:
const myOldArr = [2, 3, 6];
myOldArr.pop()
myOldArr.shift();
console.log(myOldArr);
Use them separately
Array.pop() and Array.shift() will return a value of array
your trying to use shift in number 6 so it will throw error
const myOldArr = [2, 3, 6];
myOldArr.pop();//last
myOldArr.shift();//first
console.log(myOldArr)
This question already has answers here:
Convert string array to integer array
(4 answers)
Closed 4 years ago.
I need to take the first 1000 digits of pi as strings in an array and return them into a new array as digits:
from this: ["1","4","1","5","9","2"...] (the full array of numbers is already provided in my assignment)
to this: [1, 4, 1, 5, 9, 2...]
I've tried creating a new variable with an empty array and using the .join method but when I console log it it returns the an empty array.
const strNums = ["1","4","1","5","9","2"...]
const newArray = [];
const integers = strNums.join(newArray);
console.log(newArray);
const input = ["1","4","1","5","9","2"];
const output = input.map(Number);
This question already has answers here:
How to get first N number of elements from an array
(14 answers)
Closed 5 years ago.
i would like to get the first 3 elements of an array of variable length. i've sorted my array and i would like to get a Top 3.
here's what i've done :
var diffSplice = this.users.length - 1;
return this.users.sort(this.triDec).splice(0,diffSplice)
my "solution" work only for an array of 4 element ( -1 )
Is there a better way to use the splice method ?
Thanks for your help
You could use Array#slice for the first three items.
return this.users.sort(this.triDec).slice(0, 3);
Don't you want to use a const value for diffSplice like
var diffSplice = 3;
return this.users.sort(this.triDec).slice(0,diffSplice)
try running
let arr = [1, 2, 3, 4, 5];
console.log(arr.slice(0, 3));
refer to Array Silce
Fill out the deletecount for Splice:
var sortedArray = this.users.sort(this.triDec);
return sortedArray.splice(0, 3);
check MDN
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
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);