简体   繁体   English

如何过滤javascript中的数组?

[英]How to filter an array in javascript?

This is an array:这是一个数组:

total = ["10%", 1000, "5%", 2000]

How can I filter these into two arrays like:我如何将它们过滤成两个 arrays,例如:

percentage = ["10%","5%"]
absolute = [1000,2000]

...using JavaScript array filter. ...使用 JavaScript 数组过滤器。

You should use filter method, which accepts a callback function.您应该使用filter方法,它接受一个callback函数。

The filter() method creates a new array with all elements that pass the test implemented by the provided function. filter() 方法创建一个新数组,其中包含通过所提供函数实现的测试的所有元素。

Also, use typeof operator in order to find out the type of item from array.此外,使用typeof运算符来从数组中找出项目的类型。 The typeof operator returns a string indicating the type of the unevaluated operand. typeof运算符返回一个字符串,指示未计算的操作数的类型。

 let total = ["10%", "1000", "5%", "2000"]; let percentage = total.filter(function(item){ return typeof item == 'string' && item.includes('%'); }); console.log(percentage); let absolute = total.filter(function(item){ return typeof item == 'number' || !isNaN(item); }); console.log(absolute);

 let total = ["10%", 1000, "5%", 2000]; let percents = total.filter(item => item.toString().includes('%')); let numbers = total.filter(item => !item.toString().includes('%')); console.log(percents, numbers);

You can use regular expressions since you have only strings in your array.您可以使用正则表达式,因为您的数组中只有字符串。

For % :为了 % :

total.filter(function(element){
    return /^[0-9]+\%$/gi.test(element);
});

For absolute :对于绝对:

total.filter(function(element){
    return /^[0-9]+$/gi.test(element);
});

You can use Array#reduce to split the array:您可以使用 Array#reduce 拆分数组:

 const total = ["10%", 1000, "5%", 2000]; const { absolute, percentage } = total.reduce((arrs, item) => { const key = typeof item === 'number' ? 'absolute' : 'percentage'; arrs[key].push(item); return arrs; }, { percentage: [], absolute: [] }); console.log(absolute); console.log(percentage);

Make two arrays from one array by separating number and string using advance  
js.

 let total = ["10%", 1000, "5%", 2000]; var percentage = total.filter(e => isNaN(e)); var absolute = total.filter(e => !isNaN(e)); console.log({percentage , absolute});

you can do it using你可以使用

 let total = ["10%", 1000, "5%", 2000] let result1 = total.filter(function(element){ if(typeof element == "number"){ return 1; } return 0; }) let result2 = total.filter(function(element){ if(typeof element == "string" && element.match(/\%$/)){ return 1; } return 0; }) console.log(result1, result2);

the first array filter all the number第一个数组过滤所有数字
the 2nd array filter elements which are a string and have a "%" in the end第二个数组过滤元素,它是一个字符串,最后有一个“%”

use this code使用此代码

$array = array('10%', '1000', '20%','2000');

//for show integer from array //用于显示数组中的整数

print_r(array_filter($array, function ($var) { return (stripos($var, '%') === false); })); print_r(array_filter($array, function ($var) { return (stripos($var, '%') === false); }));

//for show % array //用于显示%数组

print_r(array_filter($array, function ($var) { return (stripos($var, '%') == true); })); print_r(array_filter($array, function ($var) { return (stripos($var, '%') == true); }));

let total = ["10%", "1000", "5%", "2000"];
let percentage = total.filter(function(item){
  return typeof item == 'string' && item.includes('%');
});
console.log(percentage);
let absolute = total.filter(function(item){
  return typeof item == 'number' || !isNaN(item);
});
console.log(absolute);

For brevity, if you plan to simply clear the array (this is an example, insert your own function "e", of course):为简洁起见,如果您打算简单地清除数组(这是一个示例,当然插入您自己的 function“e”):

 let MyArray = [1,2,3,4,5] MyArray = [].concat(MyArray.filter(e => e > 2)) console.log(MyArray)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM