lodash _.pullAt在路径返回未定义 [英] lodash _.pullAt returning undefined at path

查看:82
本文介绍了lodash _.pullAt在路径返回未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在React应用程序中使用lodash更新和删除包含对象的数组中的嵌套元素,这些对象随后会更新我的组件状态并保存到localStorage。

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})
}

我是lodash的新手,但是通过路径删除元素而不返回null或undefined的最佳方法是什么?

I'm new to lodash but what's the best way to delete elements by a path without returning null or undefined?

推荐答案

_。pullAt 接受整数,而不是字符串

_.pullAt takes integers, not a string

_.pullAt(questions, 0)  
console.log(questions)  // output is []

这篇关于lodash _.pullAt在路径返回未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆