What is the process for converting an array of strings into a 2D array?

How can I transform the string

["0,1", "0,1", "1,2"]
into an array of arrays like this: [[0,1], [0,1], [1,2]]?

Answer №1

To achieve this, you can utilize the combination of Array#map and String#split.

const words = ["one,two", "three,four", "five,six"];
const result = words.map(item => item.split(",").map(Number));
console.log(result);

Answer №2

For this task, you can utilize the combination of .map() and .split() along with either .parseInt() or simply using +:

console.log(
  ["0,1", "0,1", "1,2"]
    .map(i => i.split(",")
      .map(n => +n)
    )
);

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

Is it possible to have a TypeScript Reducer alongside JavaScript Reducers in my combineReducers function?

export default combineReducers({ todosReducer, calculatorReducer, dateReducer, }); I've encountered a challenge while trying to incorporate TypeScript into a portion of my extensive codebase. In the code snippet above, envision the first two reducers ...

What methods can I employ with JavaScript to catalog data collected from an HTML form?

Currently, my form requires users to input a username that cannot start or end with a period (.). I have implemented some code but I believe there is an issue with the .value[0] parts. //Checking Username if (document.getElementById("uName&quo ...

What is the process for sending JavaScript with an Ajax request?

Working with ajax and javascript for the first time, I'm no expert in web development. Here is the code I've written and tested so far. I have a select div containing some options. <select id="month" onchange="refreshGraph()"> When an op ...

Distinguishing Between ReactDOM.render and React Component Render

As I delve into learning React, I have come across the render() method being used in two different ways: Firstly, it is used with ReactDOM.render() ReactDOM.render( < Test / > , document.getElementById('react-application') ); Sec ...

Implementing a higher-order component to attach individual event listeners to each component

One of the challenges I am facing in my app is handling user inputs from the keyboard using components. To address this, I have developed the following function: export default function withKeydownEventHandler (handler) { id = id + 1 return lifecycle({ ...

What is the most effective method for closing a DropDown when a Link is clicked in React?

Here is the code snippet I am working with: Link to Sandbox import React, { useState } from "react"; import ReactDOM from "react-dom"; import { BrowserRouter, Route, Switch, Link } from "react-router-dom"; import "./styles.css"; function DropDown({ close ...

Error in TypeScript Compiler: When using @types/d3-tip, it is not possible to call an expression that does not have a call

Seeking help to understand an error I encountered, I have read all similar questions but found no solution. My understanding of TypeScript is still growing. I am attempting to integrate the d3-tip module with d3. After installing @types/d3 and @types/d3-t ...

What is the best way to share a PHP array with Vue.js?

I recently encountered an issue with my trainers.inc.php file, which is responsible for generating an array filled with data retrieved from a database : $trainers_meta[0] = array('Id' => $id, 'Name' => $name, 'Description&apo ...

It seems that we are encountering an error while attempting to retrieve JSONP data from an

I am currently working on making a cross-domain fetch from an ASP.NET page using Jquery-JSONP. Here's the code in my ASP.NET page: public partial class Test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) ...

Changing HTML elements dynamically within an ng-repeat using AngularJS directives

I have devised an angular directive where I execute an ng-repeat. The fundamental purpose of this directive is to interchange itself with a distinct directive that depends on a value forwarded into the original directive: <content-type-directive type=" ...

Angular: Cleaning an image tag in sanitized HTML text

I have a scenario where I am integrating an HTML snippet from a trusted external source into my Angular component. To ensure security, I am utilizing Angular's DomSanitizer and specifically the bypassSecurityTrustHtml method to process the snippet bef ...

Analyzing login outcomes on a mobile app

I am currently facing an issue with parsing the success function of my ajax while trying to complete a mobile application login. Any assistance would be greatly appreciated. $(document).ready(function () { //event handler for submit button ...

HTML is not connecting to CSS

I'm having trouble linking my external CSS to my HTML file. In the head of my index.html, I have this code: <head> <title>Twenty by HTML5 UP</title> <meta charset="utf-8" /> <meta name="viewport ...

javascript The image loading function only executes once

When working with my code, I encounter an issue related to loading and appending images. Initially, I load an image using the following code: var img = new Image(); img.src= 'image.png'; Afterwards, I append this image to a div like so: $(&apo ...

The Angular framework may have trouble detecting changes made from global window functions

While working, I came across a very peculiar behavior. Here is the link to a similar issue: stackblitz In the index.html file, I triggered a click event. function createClause(event) { Office.context.document.getSelectedDataAsync( Office.Coerci ...

Can someone help me figure out how to increase the values of two specific attributes within a class?

Currently facing a challenge with adjusting the number of likes and comments using increment for properties 'numberOfLikes' and 'comments'. Unsure whether to utilize a for loop or just the increment operator. Still new to coding, so apo ...

Problem with Vue.js dropdown functionality in Internet Explorer

After developing a form using Vue.js to allow users to save and return to their answers, I encountered an issue in Internet Explorer. When the page loads, the dropdown menu tied to a computed property does not display the previously selected answer as expe ...

The error message indicates a type mismatch: it is not possible to convert from a double[][]

As a beginner in Java, I recently encountered this problem that I couldn't find a solution for after searching extensively. public class Cliente{ private int clientID; private String clientName; private double clientDebt; private dou ...

Algorithm for Navigating PHP Categories in Reverse

In my quest to enhance an e-commerce category system with unlimited category depth (pending memory constraints), I have successfully retrieved and organized all categories at once in a multi-dimensional array structure that looks something like this: [arr ...

Vue.js: Retrieving and Using Data from the API Repeatedly

<template> <h1 class="text-lg text-gray-400 font-medium">Tracker dashboard</h1> <div class="flex flex-col mt-2 w-3/4"> <div class="-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8"> ...