Lädt...


🔧 Find Array Element in the Array using JavaScript


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

The function Find searches for a specific number (NewVal)in a predefined array (data). If the number is found, it logs a message indicating its availability; otherwise, it indicates the number is not available.

Simple Example:

    function Find(NewVal) {
        let data = [
          1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 15, 16, 20, 35, 88, 90, 45, 69, 18,
          55, 100,
        ];

        // console.log(data);

        let i = 0;
        let Find = false;
        while (i < data.length) {
          // it is a testing array element print or not
          // console.log(data[i]);
          if (data[i] == NewVal) {
            Find = true;
            break;
          }
          i++;
        }

        if (Find) {
          console.log(`${NewVal} Variable is Available ..! `);
        } else {
          `${NewVal} Variable is not Available ..! `;
        }
      }

      Find(10);

Output:

10 Variable is Available ..! 

Simple Example using DOM:

This program allows users to check whether a specific element exists in a predefined array. The user enters a value into an input field and clicks a button to initiate the search. The result is then displayed on the webpage, indicating whether the entered value is present in the array.

HTML Structure:

Container: A flex container centered on the page.
Input Field: Where users can enter the number they want to search for.
Button: Triggers the search function when clicked.
Result Display: Shows whether the entered number is available in the array.

CSS Styles:

Center the container and style the input field and button for a visually appealing interface.

JavaScript Functionality:

Data Array: Predefined array of numbers.
Display Array: Shows the array on the webpage.
Search Function: Searches for the user-input number in the array and displays the result

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Find Element From the array..!</title>

    <style>
      .container {
        display: flex;
        justify-content: center;
        margin-top: 100px;
        font-size: 20px;
      }
      input {
        padding: 10px;
        width: 400px;
      }
      button {
        padding: 10px;
        background-color: blueviolet;
        border-color: blueviolet;
        color: white;
        cursor: pointer;
        font-size: 15px;
        font-family: Arial, Helvetica, sans-serif;
      }
      button:hover {
        background-color: rgb(110, 12, 202);
      }
    </style>
  </head>

  <body>
    <div class="container">
      <div>
        <h2>Find Element in Array..!</h2>

        <h3 id="NumData"></h3>
        <input type="text" id="Num" />

        <button onclick=" Find()">Find Element</button>
        <hr />
        <h3 id="Ans"></h3>
      </div>
    </div>

    <script>
      let Data = [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 15, 16, 20, 35, 88, 90, 45, 69, 18,
        55, 100,
      ];
      document.getElementById(
        "NumData"
      ).innerHTML += `Our Array is: <br> ${Data}`;

      function Find(NewVal) {
        let NewValue = document.getElementById("Num").value;

        let i = 0;
        let Find = false;
        while (i < Data.length) {
          // it is a testing array element print or not
          // console.log(data[i]);

          if (Data[i] == NewValue) {
            Find = true;
            break;
          }

          i++;
        }

        if (Find) {
          document.getElementById(
            "Ans"
          ).innerHTML = `${NewValue} is available in the array.`;
        } else {
          document.getElementById(
            "Ans"
          ).innerHTML = `${NewValue} is not available in the array.`;
        }
      }
    </script>
  </body>
</html>

Output:

Image description

Image description

...

🔧 Find Array Element in the Array using JavaScript


📈 49.98 Punkte
🔧 Programmierung

🔧 Find Array Element in the Array using JavaScript


📈 49.98 Punkte
🔧 Programmierung

🔧 Tìm Hiểu Về RAG: Công Nghệ Đột Phá Đang "Làm Mưa Làm Gió" Trong Thế Giới Chatbot


📈 39.47 Punkte
🔧 Programmierung

🔧 Select Element in Array() to a new Array() JavaScript


📈 37.67 Punkte
🔧 Programmierung

🔧 JavaScript Array Length – How to Find the Length of an Array in JS


📈 33.08 Punkte
🔧 Programmierung

🔧 Maximize Array sum by adding multiple of another Array element in given ranges


📈 31.14 Punkte
🔧 Programmierung

🔧 💡Solving LeetCode 34: Find First and Last Position of Element in a Sorted Array


📈 29.26 Punkte
🔧 Programmierung

🔧 How to Find the Element that Appears Once in an Array


📈 29.26 Punkte
🔧 Programmierung

🔧 How to Find the Largest Element in an Array


📈 29.26 Punkte
🔧 Programmierung

🔧 Find First and Last Position of Element in Sorted Array | LeetCode | Java


📈 29.26 Punkte
🔧 Programmierung

🔧 Find the Kth occurrence of an element in a sorted Array


📈 29.26 Punkte
🔧 Programmierung

🔧 Find if it is possible to reach the last element of the Array


📈 29.26 Punkte
🔧 Programmierung

🔧 Searching an Element in an Array with JavaScript


📈 28.19 Punkte
🔧 Programmierung

🔧 Deleting an Element from an Array in JavaScript


📈 28.19 Punkte
🔧 Programmierung

🔧 Finding an Element in an Infinite Array Using Java


📈 26.38 Punkte
🔧 Programmierung

🔧 Maximize minimum element of an Array using operations


📈 26.38 Punkte
🔧 Programmierung

🔧 Learn the JavaScript Array.every() and Array.some() methods


📈 25.48 Punkte
🔧 Programmierung

🔧 JavaScript: Arrays, Array Properties, Array Methods: push, pop, shift, unshift, Stacks, and Queues!


📈 25.48 Punkte
🔧 Programmierung

🔧 JS Sum of an Array – How to Add the Numbers in a JavaScript Array


📈 25.48 Punkte
🔧 Programmierung

🔧 JavaScript Array Tutorial – Array Methods in JS


📈 25.48 Punkte
🔧 Programmierung

🔧 JavaScript Sort Array - How to Sort an Array Accurately


📈 25.48 Punkte
🔧 Programmierung

🐧 Convert Array of Starings to Array of Numbers in JavaScript


📈 25.48 Punkte
🐧 Linux Tipps

🔧 how i can click a child element inside of element ?


📈 24.37 Punkte
🔧 Programmierung

🕵️ CVE-2022-22512 | VARTA Element Backup/Element S Web UI hard-coded credentials (VDE-2022-061)


📈 24.37 Punkte
🕵️ Sicherheitslücken

🕵️ Microsoft Internet Explorer 9/10 SVG Element Text Element memory corruption


📈 24.37 Punkte
🕵️ Sicherheitslücken

🕵️ Element N.v Element Instantshop 1.0 add_2_basket.asp price privilege escalation


📈 24.37 Punkte
🕵️ Sicherheitslücken

🕵️ Mozilla Firefox bis 46 DOM Element Handler mozilla::dom::Element Pufferüberlauf


📈 24.37 Punkte
🕵️ Sicherheitslücken

🕵️ Mozilla Firefox bis 46 DOM Element Handler mozilla::dom::Element Pufferüberlauf


📈 24.37 Punkte
🕵️ Sicherheitslücken

🔧 Understanding JavaScript Array Methods: forEach, map, filter, reduce, and find


📈 23.6 Punkte
🔧 Programmierung

🐧 filter vs. find: JavaScript Array Methods


📈 23.6 Punkte
🐧 Linux Tipps

🔧 Creating a Draggable Element Using HTML, CSS, and JavaScript


📈 23.42 Punkte
🔧 Programmierung

🔧 How to check if an element is visible in the viewport using JavaScript and React Hook.


📈 23.42 Punkte
🔧 Programmierung

🔧 Create a draggable element using Javascript : A beginners guide


📈 23.42 Punkte
🔧 Programmierung

matomo