sne*_*ank 6 javascript typescript lodash angular angular5
在我的应用程序中,如果数据是未定义的或来自服务的null,我的html将无法加载,并且会出现“数据未定义”错误,因此我想使用lodash,但不知道如何使用它。
在我下面的ts文件中
this._PartService.GetDataValues().subscribe(
result => {
this.distributionData = this._PartService.part_data.partMasterDataCompleteList[0].partMasterData.plantSupplies.plantSupply
}
Run Code Online (Sandbox Code Playgroud)
如果数据不存在,我将获得“ partMasterDataCompleteList”为undefiend或null,所以我在尝试使用Lodash,但我不知道如何使用它
Lodash 提供了很多方法来检查并从对象中获取您想要的值。
_.get实际上会返回该值(如果存在),undefined如果不存在则返回。
_.has将检查该值是否存在true,如果存在则返回,false如果不存在则返回。
_.hasIn会执行相同的操作_.has,但还会检查这是否是继承的属性。
_.result实际上会遍历路径并返回值,但有一个主要区别......它会在获取值的过程中执行任何函数。
例子:
var data = { more: { result: 1}}
var data2 = _.create({ 'more': _.create({ 'result': 2 }) });
var data3 = { more: function() { return { result: 1} }}
console.log(_.get(data, 'more.result')) // 1
console.log(_.has(data, 'more.result')) // true
console.log(_.hasIn(data2, 'more.result')) // true
console.log(_.result(data3, 'more.result')) // 1Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1951 次 |
| 最近记录: |