3

I am wondering if there is an easy way to splice out all elements after a key position.

array.splice(index,howmany,item1,.....,itemX)

The docs say the 2nd element that specifies the number of elements to be removed is a required field, is there a caveat to get this done?

P.S - Not looking for the normal brute force solutions.

Rohan
  • 3,296
  • 2
  • 32
  • 35
  • 1
    It's very easy try yourself in console: `var a = [1,2,3,4,5]; a.splice(2); console.log(a);`. – dfsq Jul 21 '14 at 10:01
  • 1
    What the heck are you asking here? [**Array.splice**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) is an incredibly simple method that takes a starting index and an optional "how many" argument, what is it you don't understand, and what would this "brute force" solution that you're not looking for look like ? – adeneo Jul 21 '14 at 10:01
  • *"Remove all elements after a position in a JSON array in Javascript using splice"* By the time you're using `slice`, it's not JSON anymore. JSON is a **textual** notation for data interchange. – T.J. Crowder Jul 21 '14 at 10:03
  • @adeneo really nonconstructive comment, you should read the question carefully or not comment at all, thanks anyway. – Rohan Jul 21 '14 at 10:05
  • @T.J.Crowder Yes, agree – Rohan Jul 21 '14 at 10:06
  • @Rohan: adeneo's comment was a comment, not an answer, and I see no indication that he didn't read the question. The question is quite vague, there's no reason adeneo shouldn't ask you to clarify. – T.J. Crowder Jul 21 '14 at 10:06
  • @T.J.Crowder i wasn't seeking an explanation to how splice worked in javascript, but if there was a way to use splice for the problem i had, so both my statements hold. – Rohan Jul 21 '14 at 10:08
  • @dfsq: According to [the specification](http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.12), the second argument is required. – T.J. Crowder Jul 21 '14 at 10:09
  • 1
    The operative phrase here is that you are looking to `truncate` an array after a certain position. T.J. shows how to just set `.length` to do that. Also, what you have is a javascript array. JSON is a text format. – jfriend00 Jul 21 '14 at 10:10
  • 1
    I was asking what you were really asking, as it's not easy to understand. You have "JSON arrays", but there's no such thing, and you want to use `splice`, but at the same time not use `splice`, and you don't want a "brute force solution", what ever that is, and T.J.'s solution surely is as "brute force" as they come, but it's still probably exactly what you're looking for ? – adeneo Jul 21 '14 at 10:13
  • @adeneo: *"You have "JSON arrays", but there's no such thing"* Sure there is: `"[1, 2, 3, 4]"` That's a JSON array. ;-D *(I know you know that, just trying a bit of levity.)* – T.J. Crowder Jul 21 '14 at 10:22
  • @adeneo forget it, TJ's solution isn't brute force, never said i didn't want to use splice. And i used json arrays in haste, apologies on that. – Rohan Jul 21 '14 at 10:27

2 Answers2

9

I am wondering if there is an easy way to splice out all elements after a key position in a json array.

If it's all elements after a key position, you do this:

array.length = theKeyPosition;

E.g.:

var array = [
    "one",
    "two",
    "three",
    "four",
    "five",
    "six"
];
var theKeyPosition = 3;
array.length = theKeyPosition; // Remove all elements starting with "four"

If you don't yet know the key position, in an ES5 environment (and this can be shimmed), you use filter:

var array = [
    "one",
    "two",
    "three",
    "four",
    "five",
    "six"
];
var keep = true;
array = array.filter(function(entry) {
    if (entry === "four") {
        keep = false;
    }
    return keep;
});

That's using strings, but you can easily change if (entry === "four") { to if (entry.someProperty === someValue) { for your array of objects.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

The second argument is actually optional for Array.prototype.splice() and the desired behaviour can be achieved using only the first argument.

For example (copied form the accepted answer):

const array = [
    "one",
    "two",
    "three",
    "four",
    "five",
    "six"
];
const theKeyPosition = 3;
array.splice(theKeyPosition+1); // Remove all elements starting with "four"
console.log(array);

However, I still prefer setting the length property as it's supposed to be faster (I can't find the JSPerf result, please help me here).

Read more about this on MDN or on my other answer to a similar question.

0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184