问题
I'm using lodash in a react app to update and delete nested elements in an array that contains objects which subsequently update my component state and saves to localStorage.
questions = [{"question":"","type":"text","conditions":null,"isSub":false,"subQ":[]}]
path = '[0]'
//returns [undefined x 1]
function deleteQuestion(path){
const { questions } = this.state
_.pullAt(questions, path)
this.setState({questions: questions})
}
I'm new to lodash but what's the best way to delete elements by a path without returning null or undefined?
回答1:
_.pullAt takes integers, not a string
_.pullAt(questions, 0)
console.log(questions) // output is []
来源:https://stackoverflow.com/questions/45042019/lodash-pullat-returning-undefined-at-path