数组查找,无法读取未定义的属性长度

时间:2018-11-08 11:04:00

标签: javascript

如何检查没有长度的选项?有些options估计options,另一些则没有。

当我检查options.length == 0

时,以下代码错误
const { group, data, year } = args;
return (
    data
        .find(element => {
            return element.id == year;
        })
        .options.find(element => {
            return element.id == group;
        }).options.length == 0

数据:

years: [
        {
            label: "Year 3",
            id: 1,
            options: [
                {
                    label: "Firm 01",
                    id: "xxx@xx.co.uk"
                },
                {
                    label: "Firm 02",
                    id:
                        "xxx.co.xxx@group.calendar.google.com"
                }, ....

1 个答案:

答案 0 :(得分:0)

我认为最好的方法是检查是否定义了变量,可以通过以下方式实现:

class SplashPresenter extends BasePresenter<SplashView> {

   private Scheduler mMainThread;

   private Scheduler mNewThread;

   private UserAuthService userAuthService;

  @Inject
  SplashPresenter(SplashView view, UserAuthService userAuthService, @Named(ValueConstants.NEW_THREAD) Scheduler newThread,  @Named(ValueConstants.MAIN_THREAD) Scheduler mainThread) {
        super(view);
        this.userAuthService = userAuthService;
        mNewThread = newThread;
        mMainThread = mainThread;
  }

让我知道这是否对您有用。