Breaking down a string using various separators

Here's the scenario: I have a file name in this format:

const fn = 'xy_20181023_ABCD.jpg';

My goal is to break this down into variables x, y, date, data as follows:

console.log({x, y, date, data});
// {
//     data: "ABCD.jpg"
//     date: "20181023"
//     x: "x"
//     y: "y"
// }

I'm aware that I can achieve this with the following code:

const [temp, date, data] = fn.split('_');
const [x, y] = temp[0]

However, my question is: Can this assignment be done in a single line? Extra points if it remains easy to understand.

Cheers,

P

Answer №1

Here's a suggestion to try out:

let filename = 'xy_20181023_ABCD.jpg';
const [[x, y], date, data] = filename.split('_');
console.log(x);
console.log(y);
console.log(date);
console.log(data);

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

What is the best way to showcase downloaded image files in a React application, after saving them in a server folder and storing their file paths in a database?

Overview of the Issue with Short Setup and Code Summary Setup Technologies: React.js, Node.js/Express.js & PostgreSQL. The process involves scraping an ImageUrl of a Book, downloading it, storing it in an Image Folder using Node.js, and saving the Filepa ...

What is the best way to display multiple VR scenes on a single webpage?

Currently, I am attempting to display several 360 photos utilizing the a-frame library through implementing a-scene. However, I am encountering an issue where only one scene is being rendered on my page. Are there any alternative approaches to address this ...

Limits of the window in a d3 network diagram

I'm currently working with a network diagram that consists of circle elements and lines connecting them. However, I've run into an issue where sometimes there are so many circles that they extend beyond the edge of my screen (see image attached). ...

How can you use Firestore to filter documents based on their presence in another collection?

For instance, I am looking to retrieve all users who are members of a certain "space". Here is a simplified Firestore rule example: match /users/{userId} { allow read: if exists(/databases/$(database)/documents/spaces/SPACEID/members/$(userId)); } Java ...

Tips for streamlining the filter function using replace and split:

Currently, I am utilizing a filter function alongside replace() and split(). Here is the code snippet: jQuery('table .abc').filter((i, el) => jQuery(el).text(jQuery(el).text().replace('a', 'b').split(' ')[0] + &ap ...

JavaScript: AWS Amplify: Retrieving the User ID (Sub ID) Following User Registration. Post Registration, Not to Be Confused with Sign In

Currently, I am utilizing the authentication module from AWS Amplify. One question that has been on my mind is how to obtain the userID once a user signs up. While it is possible to retrieve the ID after a user signs in, I am specifically interested in re ...

Exploring jasmine testing with ajax requests

I've been working on writing Jasmine unit tests for a JavaScript file that includes an Ajax call. I'm unsure how to properly test the 'Success' function using Jasmine. Here's a snippet of the code: function getGroups(){ var defe ...

Tips for triggering the JavaScript function within dynamically created textboxes on an ASP .NET platform

I have developed code that dynamically creates textboxes in a modal pop-up each time the add button is clicked and removes them when the remove button is clicked. The validation function in this code checks for valid month, date, and year entries in the te ...

Executing Multiple Requests Concurrently in Angular 5 using forkJoin Technique

Important Note The issue lies in the backend, not Angular. The requests are correct. In my Angular5 app, I am trying to upload multiple files at once using rxjs forkJoin. I store the requests in an array as shown in the code below. However, after adding ...

Tips for transferring objects between AngularJS directives

Is it possible to access an object named foo that is defined/instantiated inside the link function of directive A from the link function of another separate directive? ...

Exploring the capabilities of Angular through filter functions and ng-bind-html

I created an angular filter called 'strLimit' to restrict the number of characters in a string: mod.filter('strLimit', ['$filter', function ($filter) { return function (input, limit) { if (!input) return; if (input.le ...

When the width is reduced to a certain point, the display will change to inline-block, preserving the layout structure

My goal is to maintain a two-column layout for my container boxes, even when the browser width is minimized to 600px in my fiddle. The issue arises with the CSS rule display: inline-block causing the boxes to stack into a single column. Important point: I ...

Transforming instance of a Class into Json format for WebService

My goal is to retrieve an object of the Product class as a Json for a webservice. Despite successfully loading all the values into the object, I encounter an error when trying to return it as Json. Below is my method: <AllowAnonymous> <HttpGet&g ...

What is the best way to determine the total number of rows that have been generated by the Asp:Repeater?

I'm looking for a way to retrieve the total number of rows generated by the repeater control using either Javascript or JQuery. Can anyone help me with this? ...

Is it possible to transmit an array using $.ajax and specify the dataType as json?

I am currently attempting to send a JavaScript array to my .php file handler and store it in my database. Although the request is successful, it seems like my array isn't being posted/saved correctly. When I check the POST request source, it shows up ...

Retrieve specific values from columns for dynamically generated rows using JavaScript

Currently, I am in the process of creating a dynamic table using PHP. In my screenshot, there are two rows with multiple columns. Each column in both rows has the same id. For example, the column for Monday has id="mon" in the first row and also id="mon" i ...

Is it possible to pass a JSON file as a JavaScript object in Express using Node.js?

When attempting to import a JSON file into my code using the line below, I am encountering an issue where the jsonConfig variable is being populated with a JavaScript object instead of the expected config file. This allows me to directly access jsonConfi ...

Trigger sound when it is fully loaded using HTML5 and JavaScript

Looking for a way to make your HTML5 audio file automatically play as soon as it loads on the page? I've got you covered. Thanks in advance! ...

Utilize Angular, PHP, and MySQL to add data to database encountering error

Working with a controller to fetch records from a database table and bind them to textfields for insertion into another table. However, upon submission, encountering the following error: TypeError: Cannot read property 'resp_fname' of undefined ...

What is the process for bundling a separate JavaScript file with Webpack5?

I am new to the world of webpack. I have 2 javascript files and I want to designate one as the main entry file. However, for the other file, I only want to include it in specific files. For example, looking at the file structure below, main.js is my entr ...