javascript move array element to front code example

Example 1: js add element to front of array

//Add element to front of array
var numbers = ["2", "3", "4", "5"];
numbers.unshift("1");
//Result - numbers: ["1", "2", "3", "4", "5"]

Example 2: move element to the top of list javascript

const data= [{code:"001",name:"Kirk-Patrick Brown"},{code:"002",name:"Sara Brown"},{code:"003",name:"Joe Frazer"}];

const firstItem = "003";
data.sort((x,y)=>{ return x.code === firstItem ? -1 : y.code === firstItem ? 1 : 0; });

console.log(data);

Example 3: javascript move array element to front

for(var i = 0; i<$scope.notes.length;i++){
	if($scope.notes[i].is_important){
    	var imortant_note = $scope.notes.splice(i,1);
    	$scope.notes.unshift(imortant_note[0]);//push to front
	}
}