Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

return last two values of array in javascript

arr.slice(Math.max(arr.length - 5, 1))
Comment

js get last n elements of array

const cars = ["benz", "bmw", "volvo", "skoda"];

const lastThree = cars.slice(-3);

console.log(lastThree); // ["bmw", "volvo", "skoda"]
Comment

javascript get last 2 item in array

const cutOffFirstAndLastFive = (array) => {
  const [first, ...rest] = array;
  return rest.slice(-5);
}

cutOffFirstAndLastFive([1, 55, 77, 88]);

console.log(
  'Tests:',
  JSON.stringify(cutOffFirstAndLastFive([1, 55, 77, 88])),
  JSON.stringify(cutOffFirstAndLastFive([1, 55, 77, 88, 99, 22, 33, 44])),
  JSON.stringify(cutOffFirstAndLastFive([1]))
);
Comment

return last two values of array in javascript

var arr1 = [0, 1, 2, 3, 4, 5, 6, 7],
  arr2 = [0, 1, 2, 3];

document.body.innerHTML = 'ARRAY 1: ' + arr1.slice(1).slice(-5) + '<br/>ARRAY 2: ' + arr2.slice(1).slice(-5);
Comment

PREVIOUS NEXT
Code Example
Javascript :: indexof js 
Javascript :: map function with params 
Javascript :: two dimensional array in javascript 
Javascript :: could not decode base64 cloudinary 
Javascript :: check install modules npm directory 
Javascript :: javascript arrow functions to create methods inside objects 
Javascript :: js ?. 
Javascript :: update head tag metadata vue 
Javascript :: sprintf js 
Javascript :: jsdoc object destructuring 
Javascript :: array.filter in javascript 
Javascript :: array map order by timestamp reactjs 
Javascript :: jquery onchange get element 
Javascript :: js color contrast ratio 
Javascript :: shell 
Javascript :: how to check for null in javascript 
Javascript :: GET FORM VALUE 
Javascript :: message.channel.name.includes 
Javascript :: iterate table in jquery 
Javascript :: add style by classname javascript 
Javascript :: material-ui.com autocomplete grouped 
Javascript :: == vs === in javascript 
Javascript :: JavaScript Local Scope Variable 
Javascript :: asynch action redux 
Javascript :: javascript line chart 
Javascript :: useref example 
Javascript :: devtool google 
Javascript :: schema knex.js 
Javascript :: js date option 
Javascript :: findindex method javascript 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =