Use js to find the specified Element in the array and return all index examples of the element. js Array

Source: Internet
Author: User

Use js to find the specified Element in the array and return all index examples of the element. js Array

Preface

This article mainly introduces how to use js to find the specified Element in the array and return all the indexes of the element. The detailed sample code is provided in this Article, let's take a look at the detailed sample code.

Sample Code

// Search for all the existing x in the array and return an Array function findall (a, x) {var results = [], len =. length, pos = 0; while (pos <len) {pos =. indexOf (x, pos); if (pos =-1) {// exit the loop to complete the break search if not found;} results. push (pos); // store the index pos + = 1; // search from the next position} return results;} var arr = [1, 2, 3, 1, 4, 1, 4, 1]; findall (arr, 1); // return [,]

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.