简体   繁体   English

基本 Javascript,如何按数值过滤数组?

[英]Basic Javascript, How can i filter an array by numeric value?

this is my first question on this community and i'm a novice programmer with JavaScript.这是我在这个社区的第一个问题,我是一名 JavaScript 新手程序员。

I have something like this:我有这样的事情:

let dog = [["extra small", 2], ["small", 5], ["medium", 7], ["big", 9], ["extra big", 12]];

Taking the data of the previous array, i want to create a new array just with the numeric values, for example:取前一个数组的数据,我想用数值创建一个新数组,例如:

ages = [2, 5, 7, 9, 12]

I tried to use "filter", but i don't know how to properly use it, also i tried to search some references to make it work but i couldn't.我尝试使用“过滤器”,但我不知道如何正确使用它,我也尝试搜索一些参考资料以使其工作,但我不能。

Thanks in advance (and sorry for my poor english, but i suppose you get the idea).提前致谢(也很抱歉我的英语不好,但我想你明白了)。

You can first use Array#map to get just the numbers and then Array#sort to sort the numbers您可以先使用Array#map获取数字,然后使用Array#sort对数字进行排序

 let dog = [ ["extra small", 2], ["small", 5], ["medium", 7], ["big", 9], ["extra big", 12] ]; let ages = dog.map(([size, age]) => age).sort((a, b) => a - b); console.log(ages);

 let dog = [ ["extra small", 2], ["small", 5], ["medium", 7], ["big", 9], ["extra big", 12] ]; const newArr = dog.map(item => { return item[1] }) console.log(newArr);

Here are my thoughts on how to achieve this.以下是我对如何实现这一目标的想法。

  1. Using Array#Map and Array#filter .使用Array#MapArray#filter Basically, mapping each element of the array, then checking for the numeric values in the subArray using JavaScript#isNaN() and returning the numbers.基本上,映射数组的每个元素,然后使用JavaScript#isNaN()检查 subArray 中的数值并返回数字。

    • isNaN() checks if the value type is not a number. isNaN() 检查值类型是否不是数字。 !isNaN() reverses that response. !isNaN() 反转该响应。
    • flat() is used to flatten the final result to a single array. flat()用于将最终结果展平为单个数组。 Alternatively, you can change map() to flatMap()或者,您可以将 map() 更改为flatMap()
 // map(), isNaN(), filter(), flat()
 let newArr = dog.map((arr) => arr.filter((val) => !isNaN(val))).flat();
 console.log(newArr); // [ 2, 5, 7, 9, 12 ]


 // flatMap(), isNaN(), filter()
 let newArr = dog.flatMap((arr) => arr.filter((val) => !isNaN(val)));
 console.log(newArr); // [ 2, 5, 7, 9, 12 ]
  1. Using function.使用功能。 This is similar to the first, however, instead of using map() we use a Array#forEach to loop through the array.这与第一个类似,但是,我们使用Array#forEach来循环遍历数组,而不是使用 map()。
function getNumeric(array) {
    let result = [];
    array.forEach((arr) => {
        let res = arr.filter((a) => !isNaN(a));
        result.push(...(res));
    });
    return result;
}
let newArr = getNumeric(dog);
console.log(newArr); // [ 2, 5, 7, 9, 12 ]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果参数是数字,我怎样才能让雪花中的 javascript 函数返回一个值,如果参数不是数字,我如何返回另一个值? - How can I get a javascript function in snowflake to return one value if the argument is numeric or another value if the argument is not numeric? 如何按属性值过滤对象数组? 在 Javascript/ React-native - how can i filter array of objects by its property value? in Javascript/ React-native 如何在JavaScript的数组中存储值? - How can I store value in the array on the JavaScript? 如何在javascript中过滤和修改数组中的每个对象? - How can i filter and modify each object in array in javascript? 如何过滤此数据并返回与 JavaScript 中的条件匹配的数组? - How can I filter this data and return an array matching the condition in JavaScript? 如何告诉 Javascript 过滤对象数组 - How can I tell Javascript to filter array of objects 在 JavaScript 中,如何过滤具有动态条件的数组? - In JavaScript, how can I filter an array with dynamic conditions? 如何比较Javascript日期以过滤数组中的对象? - How can I compare Javascript dates to filter objects in an array? 如何使用jmesPath过滤具有数组的表达式的javascript对象? - How can I filter javascript objects with an expression that has array in it with jmesPath? 如何在 Javascript 的数组中过滤掉相似的属性名称? - How can I filter out the similar attribute names in an array in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM