typescript map doesn’t return one list

Issue

This Content is from Stack Overflow. Question asked by onwkbapzjamrhoescc

I have a service that returns a list of users, for each of users I have to calculate and get some codes based on user id and then I need to remove the last code since it’s not valid (in all cases) and then find the final code by calling another function with the list of codes(numbers).

        let finalCode: number;
        this.usersService.getUsers().subscribe(users => {
            let userCodes: number[] = [];
            for (const user of users) {
                const codes = calculateUserCodes(user.id); // this functions returns an array with numbers [5, 2, 1];
                codes.splice(-1);
                userCodes.push(...codes);
            }
            userCodes = userCodes.sort((a, b) => a-b); // it is an array e.g [2,3,1]
            finalCode = compareAndFindFinalCode(userCodes);
        });

I did this solution but not sure if it’s efficient in Angular. So instead of for loop I wanted to use map and simplify:

 let finalCode: number;
        this.usersService.getUsers().subscribe(users => {
          let a = colors.map(color => {
            const codes = calculateUserCodes(user.id); 
            codes.splice(-1);
            return codes;
          }).sort((a, b) => a-b);

     }
     finalCode = compareAndFindFinalCode(userCodes);

But this doesn’t return this error The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type. because the map creates a list of arrays instead of one array. Can someone help me solve this, please?



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?