Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move array item to front by object key

So I have an array of objects;

[
    {
        "foo": 2,
        "bar": "test"
    },
    {
        "foo": 19,
        "bar": "value"
    },
    {
        "foo": 7,
        "bar": "temp"
    }
]

I need to move an object with a particular value of foo to the beginning of the array. The value is always in the object, but there's no guarantee the object will be in the array.

So after running moveToFront(19); for instance, I'd have the following:

[
    {
        "foo": 19,
        "bar": "value"
    },
    {
        "foo": 2,
        "bar": "test"
    },
    {
        "foo": 7,
        "bar": "temp"
    }
]

How would I go about doing this?

like image 640
Emphram Stavanger Avatar asked Jan 11 '16 18:01

Emphram Stavanger


People also ask

How to move an array element from one position to another?

Also there is a method called splice () in JavaScript, by which an array can be removed or replaced by another element for an index. So to move an array element from one array position to another we can splice () method or we can simply use array indexing ( []).

How to move an element using arrow keys in jQuery?

The task is to move an element to left, right, up and down using arrow keys in jquery, we can use the jQuery keydown () method along with the .animate () method. The keydown () method triggers the keydown event whenever User presses a key on the keyboard.

How to move the element according to the key pressed?

The movement of the element according to the key pressed is done using .animate () method Example 1: Moving element to up using Up arrow key. Example 2: Moving element to Down using Down arrow keys. Example 3: Moving element to Left using Left arrow keys. Example 4: Moving element to Right using Right arrow keys.

How to move an element to left right up and down?

How to move an element to left, right, up and down using arrow keys ? - GeeksforGeeks How to move an element to left, right, up and down using arrow keys ? The task is to move an element to left, right, up and down using arrow keys in jquery, we can use the jQuery keydown () method along with the .animate () method.


1 Answers

Shortest way: Array.some

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.

Note that Array.find also breaks the iteration once target is found, but it isn't' supported on IE.

var data = [{"foo":2}, {"foo":19}, {"foo":7}, {"foo":22}]

// if {foo:7} is found, move it to the front and break iteration
data.some((item, idx) => 
  item.foo == 7 && 
  data.unshift( 
    // remove the found item, in-place (by index with splice), 
    // returns an array of a single item removed
    data.splice(idx,1)[0] 
  ) 
)


// print result
console.log(data)

If you don't want to mutate the original Array, you can do:

var data = [{"foo":2}, {"foo":19}, {"foo":7}, {"foo":22}]

// if {foo:7} is found, move it to to the front and break iteration 
const clonedData = [...data]
clonedData.some((item, i, arr) => item.foo == 7 && arr.unshift(item))

// print result
console.log(clonedData)

findIndex method will be helpful

The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.

var data = [{"foo":2}, {"foo":19}, {"foo":7}, {"foo":22}]

// find the index of the target array item:
var itemIndex = data.findIndex(item => item.foo == 7); 

data.splice(
    0,                           // new index,
    0,                           // no removal
    data.splice(itemIndex, 1)[0] // detach the item and return it
);

// print result
console.log(data)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

If you use lodash and need to have legacy browsers support, use the _.findIndex method:

_.findIndex(data, {foo:19});

This will move the Array Object with the key "foo": 19 to the beginning of the array.

like image 124
vsync Avatar answered Sep 19 '22 18:09

vsync