如何获取数组的所有索引到新数组 [英] How to get all index of an array to new array

查看:51
本文介绍了如何获取数组的所有索引到新数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要映射的数组数组.但是我的映射存在问题,因为映射所有索引不仅是我要映射的索引.我认为我可以通过仅创建要映射的数据的新数组来解决此问题.

I have an array of arrays that I am trying to map. But I am having an issue with my mapping because mapping all indexes not just the one I want it to be mapping. I figured I can work my way around this by creating a new array of only of the data I am trying to map.

如何将所有 index 2 推送到新数组中?我无法找到我要尝试做的事的例子.

How can I push all index 2 into a new array? I have not been able to find an example of what I am attempting to do.

我期望新数组的结果等于 [231、431、481]

I am expecting an outcome of a new array that equals [231, 431, 481]

这是我的代码示例:

const array = [ ["name", 1, 231], [ "name", 2, 431], ["name", 3, 481] ] 

console.log(array)

let percentage = array.map(function (num, i) {
      return 100 * ((num - array[i - 1]) / (array[i - 1]));
    });

推荐答案

您可以这样做

const array = [ ["name", 1, 231], [ "name", 2, 431], ["name", 3, 481] ] 
const arrayNew = array.map(x => x[2])
// arrayNew === [231, 431, 481]

这篇关于如何获取数组的所有索引到新数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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