How it can be more efficient?
First, we need to make it correct; for-in
isn't how you loop through arrays. The equivalent would be a simple for
loop:
for (var i = 0; i < array_object.length; ++i) {
arrayP.push(array_obectj[i].a)
}
Then, what you have is already quite efficient. JavaScript engines optimize Array#push
really well.
Other options:
Assign to the next index in the loop:
for (var i = 0; i < array_object.length; ++i) {
arrayP[i] = array_obectj[i].a;
}
Some JavaScript engines will optimize that better than push
, but some do push
better.
Use map
:
arrayP = array_object.map(e => e.a);
...but note that map
has to make calls back to your callback function. Still, browsers optimize that well, too.
However, "how can it be more efficient" is usually not the question you want to ask. "How can it be clearer?" "How can I ensure the code is easy to read?" "How can I make this easily maintainable?" are all better questions to ask. Answering those, I'd say map
is the clearest. It's a specific idiom for exactly this operation.