Determine the size of an array by utilizing the map function in ES6

Attempting to determine the length of an array using ES6 with the following code, but encountering issues.

a=[[1,2,3,4],[4,5,6]]
result = a.map(d=>({d[0]:d.length}))
console.log(result)

The correct method is:

a=[[1,2,3,4],[4,5,6]]
result = a.map(d=>({name:d[0], length:d.length}))
console.log(result)

Answer №1

To create a key for an object from a variable in ES6, you need to enclose it within square brackets [].

For instance:

const key = 'category';
const item = {
  [key]: 'food',
};

console.log(item);

Therefore, the code snippet you are interested in should look like this:

a=[[1,2,3,4],[4,5,6]]
result = a.map(arr=>({[arr[0]]: arr.length}))
console.log(result)

Answer №2

If you want to create an array of objects where the first element of each subarray becomes the property name and the length of the subarray becomes the property value, you can achieve it using the following code snippet:

let arr = [[1,2,3,4],[4,5,6]];
let result = arr.map(item => ({[item[0]]: item.length}));
console.log(result);

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

Within the materia-ui table, I am facing an issue where clicking the button to expand a row results in all rows expanding. I am seeking a solution to ensure only the selected row expands

When a row is clicked, all rows in the data table expand to show inner data for each row. The issue is that clicking the expand button expands all rows rather than just the selected row. Each time I try to expand one specific row, it ends up expanding mul ...

The display in xtermjs appears distorted, showing skewed text instead of displaying correctly

After saving data from a tmux session using capture-pane into a file.txt, the output can look like this: PC1:/path$ cd /usr PC1:/usr$ ll total 0 drwxr-xr-x 1 root root 4096 May 21 2019 [1;34m.[0;39m/ drwxr-xr-x 1 root root 4096 Aug 1 2019 [1;34m..[0;39m/ d ...

How can I change the default selected option for a radio button in a React application?

I am having trouble setting the default checked radio button to lime in my radio button group. Even though I have set lime as the default value, it doesn't seem to work. Here is the code snippet that I am working with and I am looking for a solution ...

From turning strings into integers to numerical transformations

I need help converting the string "9876543210223023" into an integer without changing its value. I've tried using parseInt, but it always converts to 9876543210223024 which is causing issues with my validations. Can someone suggest a method to keep th ...

Is there a way to identify when a specific DIV element changes its dimensions without continuously polling for updates? In other words, I am looking for a method to

Running ads on my website has been great, but one of the ads keeps changing the dimensions of the DIV it's in, causing layout issues with other elements on the page. To solve this problem, I need to add an event listener to the specific DIV element t ...

Access RSS feeds externally without relying on any third-party libraries or server-side processing

Looking to integrate an RSS parser that can parse feeds using jQuery/JavaScript without relying on the Google Feed API or any server-side solutions. The goal is to have auto-updating RSS feeds without overloading the server with multiple requests from user ...

Error Encountered When Trying to Import BrowserAnimationsModule in Angular 5: Unexpected Token "<"

As I try to integrate the BrowserAnimationModule into my Angular 5 project, a rather foolish error keeps popping up. Error: (SystemJS) Unexpected token < SyntaxError: Unexpected token < at eval (<anonymous>) at Object.eval ...

Storing HTML table data in a MySQL database will help you

Operating a website focused on financial planning where users can input various values and cell colors into an HTML table. It is crucial to uphold the integrity of these HTML tables. How can I store the complete HTML table (including values and colors) i ...

Validating Empty Fields with jQuery

When I submit the form with empty fields, a success message appears in the dialog box. However, I want to display an error message instead. Can someone please help me find a solution? Here is my HTML code: <form id="booking" action="" method="post"> ...

Experiencing disconnection from SQL server while utilizing the Express.js API

Im currently working on an API that retrieves data from one database and posts it to another database, both located on the same server. However, I am facing issues with the connections. Initially, everything works fine when I run the app for the first time ...

Utilizing NPM Script Argument Replacement

One of my npm scripts looks like this: "scripts": { "start": "webpack-dev-server --inline --config build/webpack.myconfig.js" } I want to be able to run the command with a different configuration file name instead of "myconfig", like this: npm run sta ...

Troubleshooting issue with parsing MySQL data in HTML table using Node.js

Seeking Assistance! I am currently in the process of developing a node file that displays data from MySQL on an HTML page with a search bar. My issue is that sometimes when I run the code, enter something into the search bar and hit the button, it works s ...

Creating a jQuery-powered assistance pop-up: A step-by-step guide

On various websites, you may have seen a help box labeled with a question mark icon [?], which, when hovered over, displays a small box of additional information. I discovered that these are typically created using a combination of Javascript and jQuery. ...

Blockage preventing text entry in a textarea

To enhance the basic formatting capabilities of a textarea, I implemented a solution where a div overlay with the same monospace font and position is used. This approach allows for displaying the text in the div with different colors and boldness. However ...

Can you tell me the proper term for the element that allows CSS properties to be changed after a link has been clicked?

I have integrated a horizontal scrolling date selector on my website, and it is functioning well. However, I am facing an issue while trying to change the CSS properties of a clicked date link using jQuery. Despite successfully firing the click event, I am ...

Remove browser data in PhoneGap / Prevent PhoneGap from utilizing cookies

Currently, I am in the process of creating a login system for my mobile application. After logging in to my server, it sends back a JSESSIONID that I can utilize for further authentication. The issue I am encountering is that PhoneGap is automatically st ...

Tips for showcasing req.validationError in a form

I am currently working on validating a sign-up page using node.js/express. My main goal is to show the error messages generated by req.validationErrors() directly on my form. I have opted to use ejs instead of jade, and while I found a solution for display ...

Removing HTML elements and accounting for new lines in a text box

I am currently utilizing CodeIgniter for my personal website, which includes multiple textareas. I am looking for a solution to prevent HTML tags from being stored in the database. Is there a tool available that can strip out any unwanted tags? Additional ...

Tips for transferring element attributes to ng-show in AngularJS

I have a chart in my application that looks like this: <canvas id="line" class="chart chart-line" height="250" width="{{width_chart}}" chart-data="data" chart-labels="labels" chart-series="series" chart-options="options" chart-colors="colors" chart-dat ...

What could be the reason why this function in jQuery is not functioning properly?

I am trying to duplicate the span element inside the `test` element using the .clone method. It seems like it works as expected. However, when I try to use .html in a `.click` function, it doesn't work. Can someone point out where I went wrong and sug ...