Skip to content Skip to sidebar Skip to footer

How To Map An Array To Another Array With Different Values (angularjs)

Currently I am trying to use an existing array to map it to another array in order to match what the server wants. for example... I have an array: $scope.users = [{ 'first

Solution 1:

A solution could be:

varlist = $scope.users.map(function(user) {
    return {
        name: user.firstName + ' ' + user.lastName,
        value: {
            registration: user.registrationNumber,
            first: user.firstName,
            last: user.lastName
        }
    };
});

Explanation

Array.map() iterates over the existing array and returns a new modified one.

So you have to use map on $scope.users and for each user you want to return a new object. In each iteration you have access on each user. The new objects will be stored inside list which will be an array.

Post a Comment for "How To Map An Array To Another Array With Different Values (angularjs)"