A Coder
A Coder

Reputation: 3046

jQuery move value to last position

I have the object as,

var names =["LET_ABC","PET_DEF","Num_123","Num_456","SET_GHI","RET_JKL"];

Now i have to move the value which contains "Num" to the last which means after the "Num" value there should be no values.

This is how i add the value to the array,

 result.each(function () {
        var tempObject = {},
            attributes = $(this).data();
        names.push(attributes.prefix+ '_' + attributes.value)
    });

Can i somehow manipulate the above code to make the "Num" values move at last.

I need something like,

var names =["LET_ABC","PET_DEF","SET_GHI","RET_JKL","Num_123","Num_456"];

Upvotes: 2

Views: 399

Answers (5)

Prashanth Reddy
Prashanth Reddy

Reputation: 197

for(var i=0;i<names.length;i++){
  if(names[i].match("^Num")){   
    names.push(names.splice(i, 1)[0]);
  }
}

Upvotes: 2

prasanth
prasanth

Reputation: 22490

Try this.simple use Array#filter and Array#concat

var names = ["LET_ABC", "PET_DEF", "Num_123", "Num_456", "SET_GHI", "RET_JKL"];
console.log(names.filter(a => !a.match(/(\d+)/g)).concat(names.filter(a => a.match(/(\d+)/g))))

Upvotes: 1

Darkisa
Darkisa

Reputation: 2037

Use push to add a value to the end of an array if it has the "Num" prefix and use unshift to add a value to the beginning of an array if it does not have the "Num" prefix. Working code:

var names =["LET_ABC","PET_DEF","Num_123","Num_456","SET_GHI","RET_JKL"];

$(document).ready(function(){
    result_array = [];
    $.each(names, function(index, value){
        if (value.substring(0,3)=="Num"){
            result_array.push(value);
        } else{
            result_array.unshift(value);
        };
    });
    console.log(result_array);
});

Upvotes: 0

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

Working example (with explanation in comments):-

var names =["LET_ABC","LET_DEF","Num_123","Num_456","LET_GHI","LET_JKL"];//your array

function movetoLast(names,checkword){// function with array and checking prefix

 $(names).each(function(index,value){ // iterate over array
   if(value.indexOf(checkword) >= 0){ // check value have that prefix or not in it
      names.push(names.splice(names.indexOf(value), 1)[0]); // if yes move that value to last in array
   }
 });
 
 return names; // return modified array
}


var names = movetoLast(names,"Num");// call function

console.log(names); // print modified array in console
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 1

jmtalarn
jmtalarn

Reputation: 1723

I would put the numbers in a separate array to concatenate the two arrays at the end.

result.each(function () {
            var tempObject = {}, attributes = $(this).data();
            var numsArray = [];        
            if (attributes.prefix==Num){
              numsArray.push(attributes.prefix+ '_' + attributes.value);
            }else{
              names.push(attributes.prefix+ '_' + attributes.value)
            }

            names.concat(numsArray); 
     });

Upvotes: 0

Related Questions