简体   繁体   中英

removing (slice? splice?) item from array based on index

I have an array that looks like this,

[
    "slug.current == 'current'",
    "parent->slug.current == 'parentSlug'",
    "parent->parent->slug.current == 'parentParentSlug'",
    "parent->parent->parent->slug.current == 'parentParentParentSlug'"
]

This is created via this code,

path.split('/')
    .filter(i => i)
    .reverse()
    .map((slug, i) => {
      return `${'parent->'.repeat(i)}slug.current == '${slug}'`;
    });

I am wanting to remove the "parent->slug.current == 'parentSlug'" from ever being the error I would like to disregard after the split if possible,

I though it would need to something like,

path.split('/')
        .filter(i => i)
        .reverse()
        .splice(2, 2)
        .map((slug, i) => {
          return `${'parent->'.repeat(i)}slug.current == '${slug}'`;
        });

This returns the below,

[
    "slug.current == 'current'",
    "parent->slug.current == 'parentSlug'"
]

when what I want is,

[
        "slug.current == 'current'",
        "parent->slug.current == 'parentParentSlug'",
        "parent->parent->slug.current == 'parentParentParentSlug'"
    ]

change splice to this

.splice(1,1)

first argument is the index, second one means how many items after this index should be deleted (including the index it self)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM