Use JavaScript .map to Create an Array of Objects

I can create an array of arrays from ajax results using:
myPeople = data.d.results.map(Person => [`${Person.Id}`, `${Person.FullName1}`, `${Person.Department1Id}`]);

Open in new window

but I want to create an array of objects instead; the following gives me an array of arrays of objects:
            myPeopleArray = data.d.results.map(Person => [{
                "ID": `${Person.Id}`,
                "Name": `${Person.FullName1}`,
                "Department": `${Person.Department1Id}`,
            }]);

Open in new window

Can I use map to create an array of objects and if so, what is the syntax?
LVL 11
Michael VasilevskySolutions ArchitectAsked:
Who is Participating?
 
Flabio GatesConnect With a Mentor Commented:
can you try this:
            myPeopleArray = data.d.results.map(Person => {
                "ID": `${Person.Id}`,
                "Name": `${Person.FullName1}`,
                "Department": `${Person.Department1Id}`,
            });

Open in new window

0
 
Julian HansenCommented:
I don't understand this
myPeople = data.d.results.map(Person => [`${Person.Id}`, `${Person.FullName1}`, `${Person.Department1Id}`]);

Open in new window

Is data.results not already an array of objects?
You are mapping over data.results with each array element being put into Person and then accessing
Person.id
Person.FullName1
Person.Department1Id

Which is an object.

What does your incoming source data look like?
What do you get from this?
console.log(data.results);

Open in new window

0
 
Michael VasilevskySolutions ArchitectAuthor Commented:
@Flabio Gates: you got me there - the below works:

myPeopleArray = data.d.results.map(Person => ({
           "ID": `${Person.Id}`,
           "Name": `${Person.FullName1}`,
           "Department": `${Person.Department1Id}`,
}));

Open in new window


@Julian Hansen: console.log(data.d.results) returns an array of objects, just like I want, but with a lot more attributes than I care about (see below). I'm using map to extract just the values I want to assign them to my attributes.

data.d.results
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Michael VasilevskySolutions ArchitectAuthor Commented:
Thank you!
0
 
Julian HansenCommented:
but with a lot more attributes than I care about (see below). I'm using map to extract just the values I want to assign them to my attributes.
Sorry but this does not make sense. You query a service, get a result (which contains more data than you need) go through a process of extracting the data you do need and then go through another process of consuming the data? Why not just feed the response straight to where it is needed?
0
 
Michael VasilevskySolutions ArchitectAuthor Commented:
Hi Julian, I'm using the data a few places on the site, so I think it makes sense to query the backend once, move the data into an array, and then consume it where I need it. This is SharePoint Online again so I'm only able to code the client-side and I'm looking to reduce the number of REST API calls.

Thanks for the input!

MV
0
 
Julian HansenCommented:
Ok just note you can move the data returned as is - there is no need to refactor it. But I will leave it there.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.