js find all indexes in array code example
Example 1: react-native array.filter by index arrow function
_onHandleRemoveSubmittedPicture = async (i, pictureindex) => {
console.log('_onHandleRemoveSubmittedPicture: Entering Function');
console.log('_onHandleRemoveSubmittedPicture: index passed in is: ', pictureindex);
this.setState(state => {
const selectedStoryPhotos = state.selectedStoryPhotos.filter((item, index) => pictureindex !== index);
return {selectedStoryPhotos,
};
});
console.log('_onHandleRemoveSubmittedPicture: selectedStoryPhotos after removal of index',pictureindex,'array = ',this.state.selectedStoryPhotos);
}
Example 2: javascript get index of object in array
const needle = 3;
const haystack = [{ id: 1 }, { id: 2 }, { id: 3 }];
const index = haystack.findIndex(item => item.id === needle);