Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

EDIT - 2 elegant solutions

Two elegant solutions, from Pranav and Bekim. Thanks, both tested and worked perfectly.

One

     for(var x in data)data[x].name == "other" ? data.push( data.splice(x,1)[0] ) : 0;

Two

   var res = data.slice(),
   len = res.length;

    for (var i = 0; i < len; i++) {
        if (res[i].name == 'other') {
            res.push(res.splice(i, 1)[0]);
            i--;
            len--;
        }
    }

JS TOOLS IM USING

Angular 1.5.6, lodash 4.1x

Here is the scenario I have an array of objects sorted alphabetically e.g. sortedData below etc.. However, within that array is also the catch all Other which is obviously sorted alphabetically as well. I want to remove other from the array and then move to the end of array without messing with the current sorted array.

NOTE

My current approach below works but is ugly. Does JS, angular or lodash have something more elegant?

var data = [
    {id:1,name:'apple'},
    {id:2,name:'banana'},
    {id:3,name:'other'},
    {id:4,name:'tomato'},
    {id:5,name:'strawberry'}
];

function move(array, fromIndex, toIndex) {
    array.splice(toIndex, 1, array.splice(fromIndex, 1)[0]);
    return array;
}

var moved = move(
    data,
    _.findIndex(data, ['name', 'other']),
    Object.keys(data).length
);

Ideal Outcome

 var data = [
        {id:1,name:'one'},
        {id:2,name:'two'},
        {id:4,name:'four'},
        {id:5,name:'five'}
        {id:3,name:'other'},
    ]
share|improve this question
    
You can make a copy of the data array with var copy = JSON.parse(JSON.stringify(data)); – seahorsepip Jun 10 at 2:22
    
You could copy the info, splice the array, then push the array. Or, as @e4en just posted, as I commented, you can make it a one liner to remove the copying of the info. – Jaketr00 Jun 10 at 2:26
1  
Is the array presorted, or are you sorting it, then afterwards, moving that one specific element? If you are sorting it, you could just write a custom sort that always returns that one object at the very end. – Alan Jun 10 at 2:32
    
@Alan I tried the sort approach with lodash but I keep running into issues and frankly couldn't get it work. Best I got was added twice ... original spot and end – Nolan Jun 10 at 4:10
    
I don't see any elegance on Pranav solution, I see overhead and confusion making the whole process at least 300% times slower than necessary, knowing that these kind of operations are slow by design, adding three self assignable incremental variables is a bit too much of unnecessary processing to an already not so fast native JavaScript array splice method. (and to be correct in your citation you need to switch the order of author names respectively to their corresponding js expressions / declarations). – Bekim Bacaj Jun 10 at 4:40
up vote 1 down vote accepted

JavaScript will do just fine I believe:

 for(var x in data)data[x].name == "other" ? data.push( data.splice(x,1)[0] ) : 0;
share|improve this answer
    
I suggest you read my answer and give up your small-talk and fantasies. – Bekim Bacaj Jun 10 at 16:34

Use simple for loop

var data = [{
  id: 1,
  name: 'apple'
}, {
  id: 2,
  name: 'banana'
}, {
  id: 3,
  name: 'other'
}, {
  id: 4,
  name: 'tomato'
}, {
  id: 5,
  name: 'strawberry'
}];

// store the length of array  
var len = data.length;

// iterate over all array elements
for (var i = 0; i < len; i++) {
  // check value is `other`
  if (data[i].name == 'other') {
    // if value is other then move the object
    data.push(data.splice(i, 1)[0]);
    // decrement i since the element removed
    i--;
    // decrement len to avoid moved element
    len--;
  }
}

console.log(data);

share|improve this answer
    
Aside, the unnecessary overhead imposed to js engine, the code clumsiness and completely unjustified power + time consumption this is not what the OP is asking for at all. You are NOT reordering the target element in the array of interest, but creating and recreating a new array leaving the target array / the array of interest untouched and in the same state it already was. Meaning, your "solution" begging for a down-vote ( which at this point, I will not bother giving ). – Bekim Bacaj Jun 10 at 16:30

@Pranav

OP says: Ideal Outcome ==>

 var data = [
        {id:1,name:'one'},
        {id:2,name:'two'},
        {id:4,name:'four'},
        {id:5,name:'five'}
        {id:3,name:'other'},
    ]

My output after processing it with my Spartan, e.g.:

for(var x in data)data[x].name == "other" ? data.push( data.splice(x,1)[0] ) : 0;

is exactly what the OP is after:

JSON.stringify( data );

"[{"id":1,"name":"apple"},{"id":2,"name":"banana"},{"id":4,"name":"tomato"},{"id":5,"name":"strawberry"},{"id":3,"name":"other"}]"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.