admin管理员组

文章数量:1289893

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.

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.

Share Improve this question edited Jul 21, 2014 at 10:30 Rohan asked Jul 21, 2014 at 9:58 RohanRohan 3,3323 gold badges33 silver badges35 bronze badges 12
  • 1 It's very easy try yourself in console: var a = [1,2,3,4,5]; a.splice(2); console.log(a);. – dfsq Commented Jul 21, 2014 at 10:01
  • 1 What the heck are you asking here? 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 Commented Jul 21, 2014 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 Commented Jul 21, 2014 at 10:03
  • 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 Commented Jul 21, 2014 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 e, but it's still probably exactly what you're looking for ? – adeneo Commented Jul 21, 2014 at 10:13
 |  Show 7 more ments

2 Answers 2

Reset to default 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.

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.

本文标签: Remove all elements after a position in an array of objects in Javascript using spliceStack Overflow