数组转换/操作 [英] Array transformation/manipulation
本文介绍了数组转换/操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的数组:
array1=[{value:1, label:'value1'},{value:2, label:'value2'}, {value:3, label:'value3'}]
我有第二个整数数组:
array2=[1,3]
我想在没有循环的情况下获得这个数组:
I would like to obtain this array without a loop for :
arrayResult = ['value1', 'value3']
有人知道怎么用javascript来做吗?
Does someone know how to do it with javascript ?
提前致谢
推荐答案
将array2
中的元素映射到array1
中元素的label
属性code> 和相应的 value
:
Map the elements in array2
to the label
property of the element in array1
with the corresponding value
:
array2 // Take array2 and
.map( // map
function(n) { // each element n in it to
return array1 // the result of taking array1
.find( // and finding
function(e) { // elements
return // for which
e.value // the value property
=== // is the same as
n; // the element from array2
}
)
.label // and taking the label property of that elt
;
}
)
;
没有注释,在 ES6 中:
Without comments, and in ES6:
array.map(n => array1.find(e => e.value === n).label);
这篇关于数组转换/操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文