-1

I have a custom filter called removeIndex which removes the first index of an array. I want to modify that so it first sorts the array, and then removes the first index. However, when I try to do that, my application freezes up. Here's what I have:

filters: {
    removeIndex: function(value) {
        sort(value); // this crashes my browser
        return value.slice(1, value.length);
    }
}

Why would that cause my browser to crash? Is there another way I should be doing this? I just want to sort the array before I slice it.

Update: When I do console.log(value), this is what I'm getting: enter image description here

So it's not just a flat array, there's other stuff tied to it.

Here's how I am using it:

  <tbody v-repeat="company in companies | filterBy searchText | orderBy 'name'">
    <tr>
      <td class="center aligned border" rowspan="@{{ company.applications.length }}" bgcolor="#F9FAFB"><a href="#"><strong>@{{ company.name }}</strong></a></td>
      <td><a href="#">@{{ company.applications[0] }}</a></td>
    </tr>
    <tr v-repeat="company.applications | removeIndex">
      <td><a href="#">@{{ $value }}</a></td>
    </tr>
  </tbody>

@ signs because I am using this inside of a Laravel app.

1
  • Can you include a MCVE? Commented Sep 28, 2015 at 19:46

2 Answers 2

0

assuming value is an array ?

filters: {
    removeIndex: function(value) {
        return value.sort().slice(1);
    }
}

through you might want to supply your own sorting method, as the default one is a Unicode based implementation:

filters: {
    removeIndex: function(value) {
        return value.sort(function(a,b) { // return -1,0,1 as required})
                    .slice(1);
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your response. I've tried that, and it's still the same result. For some reason, it's going into an infinite loop and i'm not sure why.
comment the return and write instead console.log(Array.isArray(value), value); to check that value is actually an array
Yep, it's returning true.
does the command [0,1,4,2].sort().slice(1) returns the array [1,2,4] in your browser ? what is your browser ?
Yes, I am using Chrome. Check my updated answer, I've provided some more details.
|
0

The issue was indeed an infinite loop. I found an issue on the Vue GitHub page which was very similar to mine:

https://github.com/yyx990803/vue/issues/1153

The solution was to first slice the array to create a copy, and then apply the sort function directly after, like so:

return value.slice().sort().slice(1)

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.