sort array based on another array js
itemsArray.sort(function(a, b){
return sortingArr.indexOf(a) - sortingArr.indexOf(b);
});/**
* Sort array of objects based on another array
*/
function mapOrder (array, order, key) {
array.sort( function (a, b) {
var A = a[key], B = b[key];
if (order.indexOf(A) > order.indexOf(B)) {
return 1;
} else {
return -1;
}
});
return array;
};
/**
* Example:
*/
var item_array, item_order, ordered_array;
item_array = [
{ id: 2, label: 'Two' }
, { id: 3, label: 'Three' }
, { id: 5, label: 'Five' }
, { id: 4, label: 'Four' }
, { id: 1, label: 'One'}
];
item_order = [1,2,3,4,5];
ordered_array = mapOrder(item_array, item_order, 'id');
console.log('Ordered:', JSON.stringify(ordered_array));
Also in JavaScript:
- filereader check file type
- Should I learn Javascript?
- slider plugin for angular
- jwt authentication node js
- react html symbol code
- react grid
- how will it look when there is a container inside a a row bootstrap
- jquery loop through list element
- javascript format date yyyy-mm-dd hh:mm:ss to dd-mm-yyyy hh:mm am/pm
- js key in dict
- javascript group by key
- how to add okta api to vuejs
- javascript can you defer inline
- event module in node js
- jquery visible
- react/redux reducers sintaxe
- how to use react fragment
- node sass version react
- reverse number in js
- javascript example code
- webslesson search bar ajax
- firebase auth sign out javascript
- Write a draw Chess Board function that accepts one parameter 'canvas' which is a canvas. It should draw an 8x8 chess board of #000 and #FFF squares. The top left corner should be a #FFF square
- concurrently node react