Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Sort an array to have specific items

const arr = [
  { flag: true, other: 1 },
  { flag: true, other: 2 },
  { flag: false, other: 3 },
  { flag: true, other: 4 },
  { flag: true, other: 5 },
  { flag: true, other: 6 },
  { flag: false, other: 7 }
];

const sortedArr = arr.reduce((acc, element) => {
  if (!element.flag) {
    return [element, ...acc];
  }
  return [...acc, element];
}, []);
Comment

PREVIOUS NEXT
Code Example
Javascript :: discord buttons 
Javascript :: xor in javascript 
Javascript :: react scroll direction 
Javascript :: Uncaught TypeError: $(...).jstree is not a function 
Javascript :: javascript foreach loop 
Javascript :: js create json from object 
Javascript :: round number to 2 symbols after comma 
Javascript :: add decimals javascript 
Javascript :: jquery validation on click 
Javascript :: c# razor for loop javascript 
Javascript :: node get current user 
Javascript :: scroll value bottom js 
Javascript :: flutter or react native 
Javascript :: palindrome number in javascript 
Javascript :: for loop in shopify liquid template 
Javascript :: jquert toggleClass condition 
Javascript :: search string for character javascript 
Javascript :: javascript equivalent of CTRL+F5 
Javascript :: python class json serializable 
Javascript :: js find duplicates in array 
Javascript :: async wait for axios reactjs 
Javascript :: Javascript screenshot in video 
Javascript :: TYPING TEXT USING JS 
Javascript :: angular material button align left 
Javascript :: file_get_contents in javascript 
Javascript :: promise with timeout js 
Javascript :: javascript array filter elements greater than 
Javascript :: javascript loop over three-dimensional array 
Javascript :: navlink activestyle not working 
Javascript :: jquery cheat sheet 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =