JavaScript to filter arrays

JavaScript to filter arrays

This article example shares the specific code for JavaScript to implement array filtering for your reference. The specific content is as follows

Today, I used my knowledge of JavaScript to create an example of filtering an array. I hope we can learn from each other and make progress together!

Final result:

js code part:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <title>Filter Array</title>
 <script>
  function Search(arr) {
   var newArr = [];
   for (var i = 0; i < arr.length; i++) {
    if (arr[i] > 10) {
     newArr[newArr.length] = arr[i];
   }
  }
   return newArr;
  }
  var arr = [1, 2, 45, 31, 7, 30, 22, 3, 5, 17];
  Search(arr);
  alert('The numbers you entered are '+arr+'\n'+'The numbers greater than 10 are: '+Search(arr));
 </script>
</head>
<body>
</body>
</html>

summary:

Filtering arrays is very common and important in various programming languages. I hope everyone can use it skillfully.

I saw a piece of code before: js filters the object array according to the array, share it with you

According to the array storing the id, filter the object with this id

var array = [
{
 time: '2020',
 id: '1',
},
{
 time: '2020',
 id: '2',
},
{
 time: '2020',
 id: '3',
}];
var filterIds = ['1', '3'];
var result = array.filter((a,i)=>{
  return filterIds.some(f=>(f === a.id)) 
})
//[{time: "2020", id: "1"},{time: "2020", id: "3"}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Several methods of traversing and filtering arrays in Jquery and traversing and parsing json objects, detailed explanation of the Map() method, and querying whether a value exists in the array
  • jQuery filtering array usage of grep, each, inArray, map and traversal of json objects
  • Several ways to traverse and filter arrays with jQuery and traverse and parse JSON objects

<<:  Tutorial on installing MySQL 5.6 using RPM in CentOS

>>:  Detailed steps to install Anaconda on Linux (Ubuntu 18.04)

Recommend

Vue event's $event parameter = event value case

template <el-table :data="dataList"&...

How to implement the King of Glory matching personnel loading page with CSS3

Those who have played King of Glory should be fam...

Vue implements drag progress bar

This article example shares the specific code of ...

WeChat applet tab left and right sliding switch function implementation code

Effect picture: 1. Introduction Your own applet n...

How to install Solr 8.6.2 in Docker and configure the Chinese word segmenter

1. Environment version Docker version 19.03.12 ce...

Introduction to Linux environment variables and process address space

Table of contents Linux environment variables and...

MySQL group query optimization method

MySQL handles GROUP BY and DISTINCT queries simil...

Common repair methods for MySQL master-slave replication disconnection

Table of contents 01 Problem Description 02 Solut...

Pure CSS3 mind map style example

Mind Map He probably looks like this: Most of the...

Detailed explanation of DOM DIFF algorithm in react application

Table of contents Preface What is VirtualDOM? Rea...

Specific use of routing guards in Vue

Table of contents 1. Global Guard 1.1 Global fron...

Front-end state management (Part 2)

Table of contents 1. Redux 1.1. Store (librarian)...

Using an image as a label, the for attribute does not work in IE

For example: Copy code The code is as follows: <...

How to install Odoo12 development environment on Windows 10

Preface Since many friends say they don’t have Ma...