The simplest way is to use _.map
on arr2
, like this
console.log(_.map(arr2, function (item) {
return arr1[item];
}));
// [ 77, 33, 8 ]
Here, we iterate the indexes and fetching the corresponding values from arr1
and creating a new array.
Equivalent to the above, but perhaps a bit more advanced, is to use _.propertyOf
instead of the anonymous function:
console.log(_.map(arr2, _.propertyOf(arr1)));
// [ 77, 33, 8 ]
If your environment supports ECMA Script 6's Arrow functions, then you can also do
console.log(_.map(arr2, (item) => arr1[item]));
// [ 77, 33, 8 ]
Moreover, you can use the native Array.protoype.map
itself, if your target environment supports them, like this
console.log(arr2.map((item) => arr1[item]));
// [ 77, 33, 8 ]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…