阵列变换/操控 [英] Array transformation/manipulation

查看:90
本文介绍了阵列变换/操控的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组像这样的:

I have one array like this one:

array1=[{value:1, label:'value1'},{value:2, label:'value2'}, {value:3, label:'value3'}]

我有整的第二个数组:

I have a second array of integer :

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 ?

在此先感谢

推荐答案

地图数组2 元素的标签与相应的数组1 元素的属性

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆