Struggling to separate a section of the array

Check out this array:

[ '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7a171319121b1f163a1f1915080a54191517">[email protected]</a>:qnyynf',
  '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f29781869790939cb297919d8082dc919d9f">[email protected]</a>:uneyrl',
  '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="264b475457534f556643454954560845494b">[email protected]</a>:gnlybe',
  '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7511101b0310073510161a07055b161a18">[email protected]</a>:eboreg',
  '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dcaeb3beb5b29cb9bfb3aeacf2bfb3b1">[email protected]</a>:zbaxrl',
  '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7d0f12041e183d181e120f0d531e1210">[email protected]</a>:gubznf',
  '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6c1a0d022c090f031e1c420f0301">[email protected]</a>:fgnejnef',
  '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="760219180f3613151904065815191b">[email protected]</a>:cnff',
  '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8efae6e1e3effdceebede1fcfea0ede1e3">[email protected]</a>:zvpunry',
  '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fd999c8b98bd989e928f8dd39e9290">[email protected]</a>:gvttre',
  '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2c4e4942464d4145426c494f435e5c024f4341">[email protected]</a>:fhcrezna',
  '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="523c33223d3e373d3c1237313d20227c313d3f">[email protected]</a>:guhaqre',
  '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbd7ced0defbded8d4c9cb95d8d4d6">[email protected]</a>:enatre',
  '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7300121d071c003316101c01035d101c1e">[email protected]</a>:zvpuryyr',
  '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="90ffe2fcf1fef4ffd0f5f3ffe2e0bef3fffd">[email protected]</a>:npprff',
  '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="25524c494750576540464a57550b464a48">[email protected]</a>:cevaprff',
  '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d5a6a1b4bb95b0b6baa7a5fbb6bab8">[email protected]</a>:cnffjbeq',
  '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="78130d0a0c110b381d1b170a08561b1715">[email protected]</a>:fhafuvar',
  '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="076362624762646875772964686a">[email protected]</a>:fhzzre',
  '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ec98858183988495ac898f839e9cc28f8381">[email protected]</a>:wrffvpn' ]

I am attempting to extract only the passwords for each email by using indexing, but it breaks when I try with commas. I'm new to working with arrays like this and could use some guidance.

How would you go about achieving this task?

Thank you in advance for your assistance!

Answer №1

Have you attempted something along these lines?

//initialize an array (as shown)
const array = ["1a:1b", "2a:2b", "3a:3b", "4a:4b"];

//map through each item in the array. 'item' here represents any item in the array.
const passwords_001 = array.map(item => {
    //Splitting each string using the colon as the separator 
    //i.e anything before or after the colon will become its own element in the 'itemAsArray' array
    var itemAsArray = item.split(':');

    //pop() removes and returns the last item of the array, assigning it to the 'password' variable
    var password = itemAsArray.pop();

    //returning the extracted password as a new item in the passwords_001 array
    return password;
});

//display the passwords_001 array to see only the extracted passwords
console.log(passwords_001);

//output
//["1b", "2b", "3b", "4b"]

Answer №2

Is this the solution you're looking for?

console.log([ '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2d40444e454c48416d484e425f5d034e4240">[email protected]</a>:qnyynf', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d3b6a0a7b6b1b2bd93b6b0bca1a3fdb0bcbe">[email protected]</a>:uneyrl', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ce1edfefdf9e5ffcce9efe3fefca2efe3e1">[email protected]</a>:gnlybe', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="debabbb0a8bbac9ebbbdb1acaef0bdb1b3">[email protected]</a>:eboreg', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cab8a5a8a3a48aafa9a5b8bae4a9a5a7">[email protected]</a>:zbaxrl', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="790b16001a1c391c1a161>"w%Va.u#V'%dAsset Lighthouse Theme
const themes = { light: 'light-theme', dark: 'dark-theme' };
// Get the user's preferred theme
const currentTheme = localStorage.getItem('theme');
// Set the initial theme based on user preference, or default to light if no preference
document.body.classList.add(themes[currentTheme] || themes.light);
// Change theme logic
function toggleTheme() {
document.body.classList.toggle(themes.dark);
document.body.classList.toggle(themes.light);
localStorage.setItem('theme', document.body.classList.contains(themes.dark) ? 'dark' : 'light');
}
</script>
</body>
</html>";}));

Or do you prefer this option instead?

console.log([ '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4c21252f242d29200c292f233e3c622f2321">[email protected]</a>:qnyynf', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6f0a1c1b0a0d0e012f0a0c001d1f410c0002">[email protected]</a>:uneyrl', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="026f637073776b714267616d70722c616d6f">[email protected]</a>:gnlybe', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e480818a928196a481878b9694ca878b89">[email protected]</a>:eboreg', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7a08151813143a1f1915080a54191517">[email protected]</a>:zbaxrl', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="30425f4953557055535f42401e535f5d">[email protected]</a>:gubznf', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3b5a2ad83a6a0acb1b3eda0acae">[email protected]</a>:fgnejnef', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cbbfa4a5b28baea8a4b9bbe5a8a4a6">[email protected]</a>:cnff', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="acd8c4c3c1cddfecc9cfc3dedc82cfc3c1">[email protected]</a>:zvpunry', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="64000512012401070b16144a070b09">[email protected]</a>:gvttre', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b393e35331;'>}<html lang="en">

<head>
<meta charset="UTF-8">
<title>Themes Tutorial</title>
<link rel="stylesheet" href="style.css">
</head>

<body onload="toggleTheme()">
<h1>Change the site theme</h1>
<button onclick="toggleTheme()">Toggle Theme</button>
<script>
// Asset Lighthouse Theme
const themes = { light: 'light-theme', dark: 'dark-theme' };
// Get the user's preferred theme
const currentTheme = localStorage.getItem('theme');
// Set the initial theme based on user preference (or default to light)
document.body.className = themes[currentTheme] || themes.light;
// Toggle between themes
function toggleTheme() {
    if (document.body.classList.contains(themes.light)) {
        document.body.className = themes.dark;
    } else {
        document.body.className = themes.light;
    }
    // Save the selected theme in local storage
    localStorage.setItem('theme', document.body.className);
}
</script>
</body>
</html>`);}));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Automatically log users out of Django and update the backend after a period of inactivity without requiring any additional requests

In my Django project, I am working on a simple multiplayer system where I need to update the isCurrentlyActive value in a user-related model automatically and then log them out. I tried using middleware following a solution from this post, which works well ...

Can you explain the functionality of the NextSibling method?

I have a question about how the property NextSibling behaves when using the method getElementsByClassName(). Let me illustrate the issue with this code example: function Sibling() { var x = document.getElementsByClassName('firstClass')[0]; ...

Having trouble installing handlebars on Node using the command line

I've been attempting to integrate handlebars with node using the instructions from my book. The guide advised me to install handlebars like so: npm install --save express3-handlebar. However, this resulted in an error npm WARN deprecated express3-han ...

To successfully handle this file type in Next.js, make sure you have the necessary loader configured as no loaders are currently set up to process this specific file

I encountered an issue when trying to load an image from a local directory in my Next.js application Failed to compile ./pages/components/image.png 1:0 Module parse failed: Unexpected character '' (1:0) You may need an appropriate loader to hand ...

Stopping the execution of jQuery().load()

The .load() feature in the jQuery library allows users to selectively load elements from another page, based on specific criteria. I am curious to know if it's feasible to stop or cancel the loading process once initiated. In our program, users can e ...

Retrieve information from the input field and then, upon clicking the submit button, display the data in the

When a user inputs their name, email, subject, text, and telephone number, I want to send an email with that data. The email will be sent to a specific email address, such as [email protected]. This process is crucial for a hotel website, where the em ...

Guide for creating a scroll-triggered rotation animation using only JavaScript

Looking to achieve a cool scroll effect with an image that rotates on the X-axis by a specific degree, such as 70deg. The goal is to have the image's rotateX value change to 0deg when it enters the viewport upon scrolling and revert back to 70deg whe ...

Define variables using specific class components only

Consider a scenario where we define a class as follows: class House { street: string; pools: number; helicopterLandingPlace: boolean; } Now, I have created a service to update my house. putHouse(house: House) { // some put request } How ...

Utilize a method in Vue.js to filter an array within a computed property

I have a question regarding my computed property setup. I want to filter the list of courses displayed when a user clicks a button that triggers the courseFilters() method, showing only non-archived courses. Below is my current computed property implement ...

Using Angular JS to connect Promises while preserving data

There have been discussions about chaining promises, but this scenario presents a unique challenge. I am currently working on making multiple http get requests in my code. The initial call returns an array, and for each object in this array, another http c ...

The initial time delay set by setTimeout does not seem to have any impact

The issue with setTimeout not working as expected lies in its execution order. The code below it gets executed without waiting for the delay specified in the first argument of 'setTimeout' to run. (function() { var a = ['#bird',&ap ...

Updating the ngModel within a directive using another directive

Struggling to grasp why modifications to an ngModel don't transfer between directives. Take a look at this plunker for a simplified example of the issue. Essentially, there's a directive I've defined that utilizes ngModel with an isolate sc ...

Select the top row of a table when the checkbox is ticked to emphasize it

Previously, I tackled a challenge on a webpage using jQuery where checkboxes in a table needed to be selected based on specific data attributes. Essentially, if one checkbox in a row was selected, the rest of the checkboxes would be disabled if their data ...

How to eliminate duplicate items in an array and organize them in JavaScript

I am looking to eliminate any duplicate items within an array and organize them based on their frequency of occurrence, from most to least. ["57358e5dbd2f8b960aecfa8c", "573163a52abda310151e5791", "573163a52abda310151e5791", "573163a52abda310151e5791", "5 ...

Utilize Javascript/Jquery to categorize JSON data based on the days of the week (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)

A function is provided below that retrieves data for a chart. The output produced by this function is structured as follows: [Object { date=Date, value=112, volume=1469}, Object { date=Date, value=124, volume=539}, Object { date=Date, value=114, vo ...

javascript - A single image within the array fails to load

I'm encountering a challenge while trying to preload images in an array for later use in drawing on a canvas, such as in a 2D top-down game board. Interestingly, one of these images (which are Greyscale GIFs, by the way) refuses to load. It's evi ...

How can I reset the DefaultValue of my Autocomplete input field after submitting?

Is there a way to reset the default value of my <TextField> inside Autocomplete after form submission? Even after submitting the form, the state of formValues remains as the default value. What can I do to fix this issue? I've attempted to mod ...

What is the best way to link the :style attribute with object syntax and incorporate multiple background images?

I'm experiencing an issue trying to bind CSS style with the object syntax to an image. The style object includes the CSS property background which refers to multiple background images, but unfortunately, these images are not showing up. Template < ...

Mastering the utilization of API routes within the Next JS 13 App Router framework

As a newcomer to React JS and Next.js, I recently made the switch from using the Page Router API in Next.js to utilizing the new App Router introduced in Next.js 13. Previously, with the Page Router, creating a single GET request involved nesting your "JS ...

Is there a way to transfer a JSON object to Excel using Nextjs/React?

How can I create a button that exports data from a JSON object to multiple file formats, including Excel (.xlsx)? "data": [ { "id": 1, "temaIndicador": "Indian", "codigo": "001", "observacion ...