Get the App
SLTechnology News&Howtos  ›  Development  › 

How to avoid using negative numbers for indexing in arrays by JavaScript

Shulou Source: shulou.com Published: 2022-06-01 04:01:01 10月03日 Update

Editor to share with you how to avoid using negative numbers in the array to do the index, I believe that most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Avoid using negative numbers as indexes in arrays var numbersArray = [1 numbersArray.indexOf 2 3 4 5]; var from = numbersArray.indexOf ("foo"); / / from is equal to-1numbersArray.splice (from,2); / / will return [5]

Note that the index parameter passed to splice is not negative; when it is negative, the element is deleted from the end of the array.

That's all of the article "how JavaScript avoids indexing negative numbers in arrays". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Negative numbers arrays indexes articles content not much elements parameters most more knowledge endings industry information information channels channels references learning help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Technology Shulou Tech Info Docker macOS