Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

return last two values of array in javascript

arr.slice(Math.max(arr.length - 5, 1))
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 :: google auth.onstatechange 
Javascript :: react scroll 
Javascript :: bootstrap dropdown doesnt work with angular 12 
Javascript :: how to concatenate strings javascript 
Javascript :: hwo to make ana array of prime numbers in javascript 
Javascript :: sort nested data using sort function javascript 
Javascript :: js pad array 
Javascript :: button not exist js 
Javascript :: javascript convert object to querystring 
Javascript :: get bottom position of element javascript 
Javascript :: object json parse nestjs 
Javascript :: react-native safeareaview 
Javascript :: react antd form disable submit button 
Javascript :: react import json 
Javascript :: react select npm 
Javascript :: javascript if object has key 
Javascript :: javascript set time to start of day 12 am 
Javascript :: check if a value is an object in javascript 
Javascript :: change class Name in react 
Javascript :: convert a string to object javascript 
Javascript :: how to export a variable in javascript 
Javascript :: angular passing data to child component 
Javascript :: string to json js 
Javascript :: password confirmation using Joi 
Javascript :: await in react in function component 
Javascript :: how to break the foreach loop in javascript 
Javascript :: how to access dictionary keys in js 
Javascript :: nodejs delete s3 folder 
Javascript :: laravel http send data json raw 
Javascript :: moment get iso week number 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =