Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js sort by array key value

myArray.sort((a, b) => a.key - b.key)
Comment

array sort by key javascript

function sortByKey(array, key) {
  return array.sort((a, b) => {
    let x = a[key];
    let y = b[key];
    
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
  });
}
Comment

sort from the key value js

var masterList = [
    { key: 1, val: "google" },
    { key: 2, val: "yahoo" },
    { key: 3, val: "msn" },
    { key: 4, val: "stackoverflow" },
    { key: 5, val: "github" },
    { key: 6, val: "jsfiddle" },
    { key: 7, val: "amazon" },
    { key: 8, val: "ebay" }
];

masterList.sort((a,b)=> (a.val < b.val ? 1 : -1)) //For Decending

masterList.sort((a,b)=> (a.val > b.val ? 1 : -1)) //For Ascending

console.log( masterList );
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to delete all slash command discord.js 
Javascript :: promise settimeout 
Javascript :: how to do regex email validation with domain 
Javascript :: js check if date object is invalid 
Javascript :: how to zip two arrays in javascript 
Javascript :: javascript reload page one time 
Javascript :: js delete dot 
Javascript :: getstaticpaths with redux 
Javascript :: pandas to json 
Javascript :: express session destroy 
Javascript :: how to get id from url in javascript 
Javascript :: jquery get date from datepicker 
Javascript :: node get unix timestamp 
Javascript :: fancybox 2 image counter 
Javascript :: nodejs release variable 
Javascript :: jquery clone 
Javascript :: check if element is array javascript 
Javascript :: node.js http request ip address 
Javascript :: next + tailwind npm 
Javascript :: discord bot javascript remove user data in array 
Javascript :: Autocomplete height adjust in materil ui 
Javascript :: if checkbox is checked 
Javascript :: react js input autocomplete off 
Javascript :: javascript work out age from date of birth 
Javascript :: most 5 spoken language in countries array in js 
Javascript :: create react app in existing folder 
Javascript :: javascript separate words by capital letter 
Javascript :: edit onclick event 
Javascript :: you have no internet connection angular 
Javascript :: if odd js 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =