A
A
ajky2016-12-13 17:29:19
JavaScript
ajky, 2016-12-13 17:29:19

How to get correct iteration number in subscribe() response?

There are N parameters to get, there is no separate request to get all of them. If you iterate over an array with parameter IDs , then in the response ( data ) the name of the last element is inserted into name, and not the name of the desired one. Can I somehow pass the key?

getCars() {
        var params_list = {
                param1:             1,  // 1 - ID параметра
                param2:             2,
                param3:             3,
                ...
                paramN:           N
            }
        
        for(var key in params_list){
            this.authHttp.get(APP_SERVER + '?car_param=' + params_list[key])
            .map( (response: Response) => response.json() )
            .subscribe(
                (data) => {
                    data["name"] = key;
                    this.carsParams.push(data);
                },
                (error) => {
                    console.log('error');
                }
            );
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kovalsky, 2016-12-13
@ajky

getCars() {
        var params_list = {
                param1:             1,  // 1 - ID параметра
                param2:             2,
                param3:             3,
                ...
                paramN:           N
            }
        
        for(let key in params_list){
            this.authHttp.get(APP_SERVER + '?car_param=' + params_list[key])
            .map( (response: Response) => response.json() )
            .subscribe(
                (data) => {
                    data["name"] = key;
                    this.carsParams.push(data);
                },
                (error) => {
                    console.log('error');
                }
            );
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question