How to get the last item of array without pop? [duplicate] - javascript

This question already has answers here:
Selecting last element in JavaScript array [duplicate]
(13 answers)
Closed 6 years ago.
I need the same result as:
var array = [1, 2, 3, 5, 7];
var top = array.pop();
The problem is that pop removes the element from the array. To fix that I added another line:
array.push(top);
But it is annoying me, I did it four or five times in this project till now. Is there a better way?

When grabbing from the end you can do array[array.length - x], where x is the number of items from the end you wanna grab.
For instance, to grab the last item you would use array[array.length - 1].

I would suggest: array[array.length-1]

You can get the arr.length starting from the end such as:
arr[arr.length -1]
Bruno,

var array = [1, 2, 3, 5, 7];
var lastItem = array[array.length - 1]

Related

How to remove first and last elements of array? [duplicate]

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)

How can I check same elements in Array? [duplicate]

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

how can i have the first 3 elements of an array of variable length in Javascript [duplicate]

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

Getting Sum for all first items in each array, all second items in each array etc. ES6 [duplicate]

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

Javascript multi Array operation [duplicate]

This question already has an answer here:
Undestanding double bracket operation in Javascript
(1 answer)
Closed 8 years ago.
I have come accross this strange use if javascript array
Can anyone explain me this.
[1,2,3][1,2]
What type of operation is this ?
In the expression
var result = [1, 2, 3][1, 2]
The first part [1, 2, 3] is an array literal, the second part [1, 2] is an index into the array. So it's equivalent to:
var array = [1, 2, 3];
var index = (1, 2);
var result = array[index];
(1, 2) is a comma expression. It evaluates its arguments from left to right, and returns the value of the last one, so it's equivalent to just 2. So the whole thing is equivalent to:
var result = array[2];
which sets result to 3.

Categories