The second array contains of search results, i.e. places that matched a search. What I want to do is to create a new array with the information
in the first array, but only for the elements that's in the second array (so in the example above only place [1] ('Karlstad') should be in the
new array.

I also want the new array to have a new structure. Instead of categories in the first level I want the places in the first levels (se example below).

You should really really really use JSON for this kind of stuff, it will be much easier for the machine and the human. You can easily convert this to JSON and vice-versa.
–
Hamed Al-KhabazJan 1 '13 at 19:46

1

+1 to @Wololo's suggestion. This type of thing is made vastly more readable by using JSON and/or JS objects.
–
bvukelicJan 1 '13 at 19:58

Thanks to Marcus' comment below for pointing out performance issues with native map.

Alternative to this is, obviously, a custom version of map that masks the for loop. So far, it seems to do insignificantly better than simply using for loop, but I have no explanation as to why, nor does it really matter.

Ok, custom map is not conclusively faster than for-loop, so let's say it performs just as well.

EDIT4:

Final version of the custom map makes it perform consistently faster than the usual for loop which increments the counter. I assume that a for loop that decrements the counter would perform just as well as the final custom map function.

@MarcusEkwall: I've updated the answer to include a custom version of map, which performs even better than for loop. ;)
–
bvukelicJan 1 '13 at 22:31

@bvukleic Nice! It's quite silly that the native Array.map is slower than the JS-implementation. Oh well, that's what happens when engine optimizations break through the overhead of using C-bindings (the same happened to PCRE). Too bad regression testing doesn't pick up on performance boosts such as these ;)
–
Marcus EkwallJan 1 '13 at 22:35

@MarcusEkwall: Yeah, I've always assumed that native code should run faster, especially since things like map can be parallelized and all that.
–
bvukelicJan 1 '13 at 22:37

@bvukleic That assumption is correct, as long as you don't count the overhead of using C-bindings. Which is probably why they are still implemented in this way :P
–
Marcus EkwallJan 1 '13 at 22:39

Here's a test case for the above. @Levi got to answer before me so I won't add another answer.
–
Marcus EkwallJan 1 '13 at 19:47

1

Don't use .each as a drop-in replacement for an inline for-loop. The tiny gain in readability (in this case) is not worth the immense drop in performance. We are talking about ~90% slower here :)
–
Marcus EkwallJan 1 '13 at 22:00

I completely agree Marcus, in most cases including this one (since we don't know how many places we are working with). However, the convenience and readability is often worth it to me when I know I'm working on a small data set.
–
LeviJan 1 '13 at 22:07

I believe most people lack the knowledge and use jQuery and other frameworks whenever possible with no thoughts on performance. But as long as you are aware of its impact I won't object.
–
Marcus EkwallJan 1 '13 at 22:20

I suspect the reason why you may be having difficulty with this lies in the fact that you are misusing arrays. Quite simply, that is the incorrect data structure to use here. The information can be more logically represented by JSON.