Angular将单个对象推入数组

时间:2017-08-28 19:01:14

标签: angular typescript

我正在尝试为搜索我的员工表创建一个组件。我有一个连接到我的服务的下拉列表,当您键入时,它会更新选项以供选择。

我遇到的问题是当记录集缩小到足以仅提供单个结果时。因为我在这里调用.map函数,所以当我们不再检查数组而不是单个对象时它会中断。

我的目标是,当检测到单个对象时,我只需将其推入数组,留下一个单个对象的数组。

 this._mapsService.searchEmployees(searchString)
                .then((results: EmployeeRecord[]) => {

                    // No Results Found
                    if(typeof(results) === undefined){
                        console.log('No Results')

                    // Single record, add object to array
                    }else if(!this._utils.isArray(results)){
                        console.log('here');
                        (results: EmployeeRecord[]) => Array(results)
                    }

                    this.employeeSelection.items = results.map(result => {
                        return new EmployeeOption(result.QID, result.FirstName + ' ' + result.LastName + ' (' + result.NTID + ')')
                    });

                    (<any>this.employeeSelection).open()
                })

参考实用程序服务:

/**
 * Determine if the obj provided is an array
 * 
 * @param {*} obj 
 * @returns 
 * @memberof UtilsService
 */
isArray(obj: any) {
    return Array.isArray(obj)
}

我的问题发生在评论// Single record, add object to array的行中。我试图将我的对象推入一个数组。我认为它是console.log函数,但我继续收到关于.map的错误,所以我知道结果没有按预期放入数组中。

我在这里缺少什么?

更新

服务电话

/**
 * Search the employee table for data
 * 
 * @returns 
 * @memberof MapsService
 */
searchEmployees(query) {
    // Minimum of 2 characters required for search
    if(query.length >= 2){
        return new Promise((resolve, reject) => {
            this._http.post(this.baseUrlGlobal + '/fetchEmployeesAPI', { "query": query}, { "headers": this.headers })
                .map((result: Response) => result.json())
                .subscribe((results) => resolve(results.results));
        });
    }
};

0 个答案:

没有答案
相关问题