Question: Trying to find the string/value in the given array and replace it with new value; 
More like toggling the sorting value and pushing it to the array;

[DEMO](http://plnkr.co/edit/huEip51x6eXcqVc5MSPe?p=preview)

Value needs to checked on the click;

```
if(_.contains($scope.sortList, sortText)){
    console.log("exist");
  } else{
    console.log("doesnt exist");
  }

```

Answer: 
Hey bro you would have searched previous questions, check out below fiddle it sorts the array string/value now you need to use underscore to find that element use contains. 
[fiddle][1] hope this will help you. all the best.

```
[1]: http://jsfiddle.net/mkdskd/78FVh/1/

```
I would do the following:

create an object with all sortable fields, holding their state:

```
var fields = {
  name: null,   // null, 'asc', or 'desc'
  age: null,    // null, 'asc', or 'desc'
  gender: null  // null, 'asc', or 'desc'
};

```

create a `toggle` function:

```
function toggle(fieldName) {
  switch (fields[fieldName]) {
    case 'asc':  fields[fieldName] = 'desc'; break;
    case 'desc': fields[fieldName] = null;   break;
    default:     fields[fieldName] = 'asc';  break;
  }
}

```

and create a function to generate the sort object from the `fields` object:

```
function getSorting() {
  var sort = [];
  for (var fieldName in fields) {
    var value = fields[fieldName];
    if (value != null) {
      sort.push(fieldName + ':' + value);
    }
  }

  return {sort: sort};
}

```

So now for example on click of button 'gender', you can call `toggle('gender')`, and after that get the new sorting object via `getSorting()`.

Alternatively, you could just remove the existing sort value from the array by simply looping over it:

```
function removeFieldsStartingWith(array, str) {
  var i = 0;
  while (i < array.length) {
    if (array[i].indexOf(str) === 0) {
      array.splice(i, 1);
    }
    else {
      i++;
    }
  }
}

```

And use this like `removeFieldsStartingWith(myArray, 'gender:')`