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

Encountered an error while compiling the ./src/App.js file - Module could not

Encountering a challenge while attempting to run my system on the local server. After running npm install, followed by npm run start, I encountered the following error: Failed to compile ./src/app/App.js Module not found: Can't resolve 'assets/ ...

How to implement an instance method within a Typescript class for a Node.js application

I am encountering an issue with a callback function in my Typescript project. The problem arises when I try to implement the same functionality in a Node project using Typescript. It seems that when referencing 'this' in Node, it no longer points ...

Exploring the functionalities of the componentDidUpdate() method in React JS

In my project, I am trying to dynamically change an API parameter using a click function and render new data accordingly. The issue I encountered is that when I trigger the componentDidUpdate method with an onclick event listener, the first click works fin ...

Is there a way to set a custom width or make the description responsive?

Is there a way to ensure that the description is responsive and automatically goes to the next line instead of extending horizontally? Although using textField could fix this issue, I also need to adjust the padding and outline. Are there any alternative s ...

Partial implementation of jQuery datepicker feature facing technical issues

Greetings, I have a functional datepicker implemented in my code as follows: <link rel="stylesheet" href="uidatepicker/themes/blitzer/jquery.ui.all.css"> <script src="uidatepicker/jquery-1.5.1.js"></script> <script src="uidate ...

The workings of the toString() function within Objects

Recently while delving into a book on Js, I stumbled upon the intriguing topic of Object to primitive conversion. The author made an interesting point in the book: For historical reasons, if toString or valueOf returns an object, there’s no error, but ...

What methods can I use in Mocha with Sinon for testing multiple callbacks?

My code involves a function with asynchronous operations and multiple callbacks var f = (cb1, cb2) => { return new Promise((resolve, reject) => { /* ... */ }); }; During testing, I wanted to use sinon to create spies var cb1Spy = sinon.spy(); va ...

Is there a way to make a React Component to update and navigate to a specific position using react-sound

Currently, I am in the process of constructing an audio player utilizing react-sound. One feature I aim to incorporate is the ability to return to a specific position within the audio track. At the moment, this is my approach: goToVeryCustomPosition() { ...

Tips on how to transform an <input type="text"> element into a <tr> element using jquery

I am currently working on updating a row using jQuery Ajax. Whenever the Edit button is clicked, it switches the <tr> element to <input type='text'>. I have utilized the .Change method which triggers successfully, but my goal is to r ...

How to manage UNC paths and "mapped network drives" within an Electron application?

I have developed a cross-platform (macOS-Windows) app using Electron that functions smoothly with files and media assets from a local drive. However, it encounters issues when dealing with UNC paths and "mapped network drives". Since I am a contractor, I d ...

Update WooCommerce Mini-cart with ajax refresh

I'm having an issue with my custom plugin where everything is working properly, except for the fact that the mini cart is not updating after adding items. I have tried various methods to trigger a refresh, but so far nothing has worked. Below is a sni ...

Using jQuery to adjust the length of a string to fit within a specific width

I am working with a table and need to input strings in each cell, but they are wider than the cell width. I want to shorten the strings without breaking lines, and add '...' at the end to show that the string is long. The table consists of aroun ...

Testing React components with Jest by mocking unnecessary components

Consider a scenario where we have the Page component defined as: const Page = () => <> <Topbar /> <Drawer /> <Content /> </> When writing an integration test to check interactions within the Drawer and Con ...

Tips for reformatting table row data into multiple rows for mobile screens using ng-repeat in Angular

Just started using Angular JS and I have some data available: var aUsers=[{'name':'sachin','runs':20000},{'name':'dravid','runs':15000},{'name':'ganguly','runs':1800 ...

Utilizing HTML5 Canvas for Shadow Effects with Gradients

Surprisingly, it seems that the canvas API does not support applying gradients to shadows in the way we expect: var grad = ctx.createLinearGradient(fromX, fromY, toX, toY); grad.addColorStop(0, "red"); grad.addColorStop(1, "blue"); ctx.strokeStyle = gra ...

Delete any HTML content dynamically appended by AJAX requests

I'm currently working on a page dedicated to building orders, where the functionality is fully dependent on ajax for finding products and adding them dynamically. However, I encountered an issue when attempting to remove an item that was added via aj ...

Encountering an issue with Material-UI and Next.js: "TypeError: theme.spacing is not a function

Encountering an issue after modifying _app.js to dynamically generate a material UI theme. I've been following the implementation example provided by the material-ui team at: https://github.com/mui-org/material-ui/tree/master/examples/nextjs. To summ ...

Graph not displaying dates on the x-axis in flot chart

I've been attempting to plot the x-axis in a Flot chart with dates. I've tried configuring the x-axis and using JavaScript EPOCH, but have had no success so far. Here's a snippet of my code: <?php foreach($data[0] as $i => $d){ ...

Is there a way to programmatically display an edit text field on an HTML page?

Currently, I have a straightforward task which involves pulling data from a MySQL database to display in an HTML table. Here is the current script: <html> <body> <table border='1'> <?php mysql_connect('url',&ap ...

Using method as a filter in AngularJS: A guide to implementing custom filters

I've created a custom data type called Message: function Message(body, author, date) { this.body = body; this.author = author; this.date = date; this.stars = []; } Message.prototype.hasStars = function() { return this.stars.lengt ...