lodash _.pullAt returning undefined at path

爷,独闯天下 提交于 2019-12-11 16:03:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!