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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.