Im doing a search through an object data response and detecting if it includes 'wdt'.. This works fine; however I am struggling to destroy or remove the found items from being processed with my data
.
Don't believe there is an effective single keyword like delete to do this in JavaScript?
I am trying splice
currently, however it doesn't seem effective. Still finding the items in my console.log(data);
let data = await getData();
filterChkpt();
function filterChkpt(){
for (let i = 0; i < data.length; i++) {
if (data[i].url.indexOf('wdt') > -1) {
console.log(data[i]);
data[i].splice(index, 1); // here would like to remove matches
} else {
// console.log('else: ', data[i].url);
}
}
}
console.log(data);
if (!this.data) {
this.data = {};
}
this.data.storage = new Memory({ data });
return this;
Unless there's some reason you need to mutate the original object, this is what array.filter is for. (And it spooks me to modify the object while iterating over it.)
const data = Array.from({length: 10}, () => ({ url: Math.random() > 0.5 ? 'foo' : 'bar' }));
console.log(data);
console.log(data.filter(x => x.url === 'foo'));