0

I'm trying to find a way to move an object to the end of the array

I have this array of objects:

[
  {"id":"4","name":"Boaz"},
  {"id":"2","name":"Shareen"},
  {"id":"3","name":"Simon"},
  {"id":"1","name":"Miriam"}
]

I want to move the whole set {"id":"3","name":"Simon"} to the end of it all. I have solution for this here. But my problem is every time that particular object is not coming in second position is there a way to check the object of id=3 and shift that to end using underscoreJS

2
  • 1
    Have you tried anything? I'm not sure what you've tried that is causing the problem. Commented Feb 27, 2017 at 6:27
  • Possible duplicate of Move object in array to end Commented Feb 27, 2017 at 6:29

7 Answers 7

8

You can just sort array.

Explanation

Array.sort expects 3 possible values as return value.

  • 1: means a is greater than b and a will be move to a higher index than b
  • 0: means a is equal to b and no change will be made.
  • -1: means a is less than b and a will be move to a lower index than b.

For more information please refer: How does Javascript's sort() work?

var data = [
  {"id":"4","name":"Boaz"},
  {"id":"2","name":"Shareen"},
  {"id":"3","name":"Simon"},
  {"id":"1","name":"Miriam"}
]

data.sort(function(a,b){
  return a.id == 3 ? 1 : 0
})

console.log(data)


An alternate method could to filter out objects with necessary id, remove them from current position and the push them back, but this is too much work in my understanding. Sort is ideal way to move object up or down the order.

Sign up to request clarification or add additional context in comments.

14 Comments

can you explain what return a.id == 3 ? 1 : 0? didn't downvote
I don't think this answers his question, he is looking for something that moves the element with id: 3 to the end. yours would not work for something like [{id:3},{id:4}]. edit: in fact yours does not move the element with id: 3 to the end of the array.
@Rajesh my mistake, you are correct, I misread your sort condition. Apparently I can't change to an upvote unless you edit your answer... Maybe you could add a period to your last sentance ;) edit: upvoted
your sort fails on var data = [ {"id":"4","name":"Boaz"}, {"id":"3","name":"Simon"}, {"id":"2","name":"Shareen"}, {"id":"1","name":"Miriam"} ]
I think @NinaScholz is right and we are both wrong every browser or environment may use deferent sorting algorithm for deferent case the JavaScrip let you defines the behaviour of the compare function, not how the actual sort is implemented.
|
3

Do some simple array manipulation

var obj = [{
    "id": "4",
    "name": "Boaz"
  },
  {
    "id": "2",
    "name": "Shareen"
  },
  {
    "id": "3",
    "name": "Simon"
  },
  {
    "id": "1",
    "name": "Miriam"
  }
];
obj.forEach(function(v, i) {
  if (v.id == 3) {//test to see if the id is 3
    obj.push(obj[i]);//push the object to the last position
    obj.splice(i, 1);//remove the object from the current position

  }

});
console.log(obj);

Comments

3

I suggest to use the delta of the comparison, not only if you have more than one true comparison, but you need a symetrically comparison, if b contains the item to sort at the end, then you get the order, you want.

var array = [{ id: "4", name: "Boaz" }, { id: "2", name: "Shareen" }, { id: "3", name: "Simon" }, { id: "1", name: "Miriam" }];

array.sort(function (a, b) {
    return (a.id === '3') - (b.id === '3');
});

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

5 Comments

what is the deferent ?!! why would you sort the last elements in the array ?!
@fareednamrouti, i don't get you, what is the question?
I mean why would we need to check if b.id === "3" if It is already in higher position ?!
how do you know that, that the only the left part is important? the sort mechanism of the language is vendor specific. if you rely only on one side, you could get the wrong result.
I checked the documentation and you are 100% right. The JavaScript standard only defines the behaviour of the compare function, not how the actual sort is implemented. That parts depends on the vendor/environment.
1

While there isnt an "out-of-the-box" underscore function, we can extend the base library with our own function using the _.mixin() function.

The extension works by using the underscore _.indexOf() function to return the index of an element that matches a predicate. We then use the native JS splice function, to remove 1 item at the returned index (Note that this will leave the array unaffected if _.indexOf returns -1). As per the docs, splice, returns an array of all elements that were removed. We, lastly use the native JS concat function, which merges two (or more) arrays together, to put the returned value from the concat on to the end of the supplied array (arr).

UnderscoreJS extending

(function(_) {
    "use strict";
    _.mixin({
        moveToEndWhere: moveToEndWhere
    });

    /**
     * @function moveToEndWhere
     * @desc Searches an array for the first item matching a predicate, and moves the matched element to the end of the array
     * @param {array} arr Array to be searched and altered
     * @param {function|object} predicate Function or object used to search for matching element
     * @returns {array} Updated array
     * @memberof _.mixin
     */
    function moveToEndWhere(arr, predicate){
        return arr.concat(
            arr.splice(
                _.indexOf(arr, predicate)
                , 1
            )
        );
    }
})(_);

Usage

var data = [
    {"id":"4","name":"Boaz"},
    {"id":"2","name":"Shareen"},
    {"id":"3","name":"Simon"},
    {"id":"1","name":"Miriam"}
];

data = _.moveToEndWhere(data, {"id":3});

Comments

0
function findIdIndex(id, array) {
  return array.findIndex(elem=>elem.id===id);
}

This will give you the position of the set with the given id, the answer to your previous question covers the rest of what you want to do.

Comments

0

first write a method like this,send the value as '3' and get the index and attr as id

function getElementIndex(array, attr, value) {
    for(var i = 0; i < array.length; i += 1) {
        if(array[i][attr] === value) {
            return i;
        }
    }
    return -1;
}

By this method you can use any property to get the data with.

Then write

var index=getElementIndex(array,'id','3');//here array is your data array you have 
var itemToReplace = array.splice(index, 1); 

array = array.concat(itemToReplace);

Comments

0

Can you try this with the getting index of the respective object and then check the position:

var array  = [{ "id": "4", "name": "Boaz" }, { "id": "2", "name": "Shareen" }, { "id": "3", "name": "Simon" }, { "id": "1", "name": "Miriam" }];

var index =  array.findIndex(x => x.id == 3);

if(index === 2) {
 var person =  array.splice(index, 1);
 array  = array.concat(person);
}

console.log(array)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.