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.