Skip to content

The Caveats2 in List Rendering, example1.items.splice function can't achive as exprected in some cases #963

Closed
@aswind7

Description

@aswind7
When you modify the length of the array, e.g. vm.items.length = newLength

To deal with caveat 2, you can also use splice:
example1.items.splice(newLength)

But when the newLength Is greater than the current length, this methods failure.
for example

qq20170619-095112 2x

Activity

chrisvfritz

chrisvfritz commented on Jun 21, 2017

@chrisvfritz
Contributor

I just update the docs to remove the also in you can also use splice. Hopefully, this makes it clearer that we recommend splice as an alternative to directly setting the length of an array.

As an aside, I don't actually recommend setting length to increase the size of an array in JavaScript. What you get at the end is not actually an array with 8 indices pointing to the value undefined, as it would appear, but rather 8 indices with undefined pointers. The difference is subtle, but has important implications. For example, if you try to map over this array with a.map(() => 3), what you'll get is [3, 3, undefined x 8], rather than [3, 3, 3, 3, 3, 3, 3, 3, 3, 3] as you might expect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @chrisvfritz@aswind7

        Issue actions

          The Caveats2 in List Rendering, example1.items.splice function can't achive as exprected in some cases · Issue #963 · vuejs/v2.vuejs.org