how to map an array to another array with different values (angularJs)

Viewed 12688

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 = [{
        "firstName": "john",
        "middleName": null,
        "lastName": "doe",
        "registrationNumber": "334"
    },
    {
        "firstName": "paul",
        "middleName": null,
        "lastName": "dean",
        "registrationNumber": "223"
    },
    {
        "firstName": "andrew",
        "middleName": null,
        "lastName": "mac",
        "registrationNumber": "132"
    }
]

however I want it to look like this...

[{
    name: "john doe",
    value: {
        registration: 334,
        last: "doe",
        first: "john"
    }
}]

So what I've been doing is something like this to map it out...but it only gets the first one.

var list = [{
    name: $scope.users[0].firstName + ' ' + $scope.users[0].lastName,
    value: {
        registration: $scope.users[0].registrationNumber,
        first: $scope.users[0].firstName,
        last: $scope.users[0].lastName
    }
}];

I tried to use angular.forEach to get all the list and push it...but hasn't really worked too well yet... any help would be great!

1 Answers
Related