I have an array of object [{a:1},{a:2},{a:3},{a:4},{a:5}]
.
I want to find index of {a:3}
and remove all next elements from index of {a:3}
.
Result should look like [{a:1},{a:2},{a:3}]
and removed elements will be {a:4},{a:5}
.
I have an array of object [{a:1},{a:2},{a:3},{a:4},{a:5}]
.
I want to find index of {a:3}
and remove all next elements from index of {a:3}
.
Result should look like [{a:1},{a:2},{a:3}]
and removed elements will be {a:4},{a:5}
.
One line vanilla JS with slice
and findIndex
const data = [{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }, { a: 5 }];
const res = data.slice(
0,
data.findIndex((el) => JSON.stringify(el) === JSON.stringify({ a: 3 })) + 1
);
console.log(res);
You can use Array#findIndex()
to find that object by its number, or Array#indexOf()
if you have a reference to it.
Then, you can either set the array's length (mutating) or use Array#slice()
(not mutating), to reduce its length:
const array = [{a:1},{a:2},{a:3},{a:4},{a:5}]
const i = array.findIndex(({a}) => a === 3)
const arrayCopy = array.slice(0, i + 1)
//or:
array.length = i + 1
console.log(i)
console.log(arrayCopy)
console.log(array)
Or with Array#indexOf()
:
//If you have a reference from somewhere:
const ref = {a:3}
const array = [{a:1},{a:2},ref,{a:4},{a:5}]
const i = array.indexOf(ref)
const arrayCopy = array.slice(0, i + 1)
//or:
array.length = i + 1
console.log(i)
console.log(arrayCopy)
console.log(array)
Alternatively, if you don't have a reference, but it's guaranteed that the array is sorted, you can use a binary search, that makes this much faster for large arrays:
function binarySearch(array, target, cb) {
let start = 0, end = array.length
while(start < end){
const middle = ((end - start - 1) >>> 1) + start
const middleVal = cb(array[middle], middle)
if(middleVal === target) return middle
if(middleVal < target)
start = middle + 1
else
end = middle
}
return -1
};
const array = [{a:1},{a:2},{a:3},{a:4},{a:5}]
const i = binarySearch(array, 3, ({a}) => a)
const arrayCopy = array.slice(0, i + 1)
//or:
array.length = i + 1
console.log(i)
console.log(arrayCopy)
console.log(array)