Home Web Front-end JS Tutorial What is a pseudo array in js

What is a pseudo array in js

Apr 14, 2018 am 09:13 AM
js

The pseudo-array in js means that you cannot directly call the array method or expect any special behavior of the length attribute, but you can still traverse them through the traversal method, typically the argument parameter of the function.

What is a pseudo array in js

This time I will bring you JS pseudo-array usagedetailed explanation, and notes on the use of JS pseudo-arrayWhat are they? The following is a practical case. Let’s take a look.

What is a pseudo array in Javascript?

Pseudo array (array-like): You cannot call array methods directly or expect any special behavior from the lengthproperty, but you can still traverse them with real array traversal methods.

1. Typical is the argument parameter of the function,
2. Like calling getElementsByTagName, document.childNodes, etc., they all return NodeList objects, which are pseudo arrays.

So how to convert a pseudo array into a standard array?

You can use Array.prototype.slice.call(fakeArray) to convert the array into a real Array object.

For example, use pseudo arrays to implement the sum of indefinite parameters.

nbsp;html>


<meta>
<title>伪数组</title>

<script>
  function add(){
    var sum=0;
    console.log(arguments);
    for(var i=0;i<arguments.length;i++){
      sum +=arguments[i];
    }
    return sum;
  }
 console.log(add(1,2,5,8));
</script>


Copy after login

operation result:

What is a pseudo array in js

Convert a pseudo array into a standard array

nbsp;html&gt;


<meta>
<title>伪数组</title>

<script>
  function add(){
    var sum=0;
    console.log(arguments instanceof Array);//可以判断下此时是不是真正数组,返回值为false;
    console.log(arguments);//此时打印的是传入的参数1,2,5,8
    var arguments=Array.prototype.slice.call(arguments);//将伪数组转化为标准数组
    arguments.push(10);//此时就可以调用标准数组的方法
    console.log(arguments instanceof Array);//可以判断下此时是不是真正数组,返回值为true;
    console.log(arguments);//此时打印的是传入的参数,push之后的数组1,2,5,8,10
    for(var i=0;i<arguments.length;i++){
      sum +=arguments[i];
    }
    return sum;
  }
 console.log(add(1,2,5,8));
</script>


Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

The above is the detailed content of What is a pseudo array in js. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use JS and Baidu Maps to implement map pan function How to use JS and Baidu Maps to implement map pan function Nov 21, 2023 am 10:00 AM

How to use JS and Baidu Maps to implement map pan function

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Recommended: Excellent JS open source face detection and recognition project

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts

How to create a stock candlestick chart using PHP and JS How to create a stock candlestick chart using PHP and JS Dec 17, 2023 am 08:08 AM

How to create a stock candlestick chart using PHP and JS

How to use JS and Baidu Maps to implement map polygon drawing function How to use JS and Baidu Maps to implement map polygon drawing function Nov 21, 2023 am 10:53 AM

How to use JS and Baidu Maps to implement map polygon drawing function

How to use JS and Baidu Map to implement map click event processing function How to use JS and Baidu Map to implement map click event processing function Nov 21, 2023 am 11:11 AM

How to use JS and Baidu Map to implement map click event processing function

What does the new operator do in js What does the new operator do in js Nov 13, 2023 pm 04:05 PM

What does the new operator do in js

See all articles