Utilize the power of lodash to group an object by multiple keys simultaneously

The information is structured as follows:

const dataset = [
  {'x': '1', 'y': '2', 'z': '3'},
  {'x': '10', 'y': '20', 'z': '30'}
]

I am aiming for the following structure:

const xArray = ['1','10']
     ,yArray = ['2', '20']
     ,zArray = ['3', '30']

This approach was taken:

...
return {
  xArray: _.values(_.mapValues(dataset, 'x'))
  yArray: _.values(_.mapValues(dataset, 'y'))
  zArray: _.values(_.mapValues(dataset, 'z'))
}

While it successfully achieves the desired outcome, there may be room for a cleaner implementation. What would be the most efficient way to group an object by multiple keys?

Answer №1

To accomplish this task, you can utilize JavaScript in its simplest form:

let information = [
  {'name': 'John', 'age': '30', 'gender': 'Male'},
  {'name': 'Jane', 'age': '25', 'gender': 'Female'}
]

const nameArray = information.map(person => person.name);
const ageArray = information.map(person => person.age);
const genderArray = information.map(person => person.gender);

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

Determining the total number of permutations of size N that match the original Array A after a function is applied to all subarrays of A

Given a permutation A of size N with distinct numbers from 0 to N-1, the MEX(A1, A2, , Ak) is the smallest non-negative integer that does not include in A1, A2, • • • , Ak. A permutation P of size N is considered identical to A if the MEX of every su ...

The error message "ReferenceError: _ref is not defined"

My journey into React began with just basic knowledge about JavaScript. I am currently working on a server query that handles MySQL, and while the connection is fine, I am encountering an issue with the return. Instead of receiving a JSON response as expec ...

Guide on utilizing exported API endpoint in Node and Express

Seeking a deeper understanding of express and its utilization of various endpoints. Recently came across an example of an endpoint that reads in a json file, demonstrated as follows: const fs = require('fs'); const path = require('path&apos ...

The intersectObjects function is failing to retrieve the object from the OBJMTLLoader

Within my scene, I've introduced a new object along with several other cubes. To detect collisions, I'm utilizing the following code snippet that fires a Ray: var ray = new THREE.Raycaster(camera.position, vec); var intersects = ray.intersectObj ...

Tips for creating horizontal dividers with CSS in Vuetify using <v-divider> and <v-divider/> styling

Currently, I am working on a project using Vue.js and adding Vuetify. However, I need to use a component. .horizontal{ border-color: #F4F4F4 !important; border-width: 2px ; } <v-divider horizontal class=" horizontal ...

Executing NodeJS commands and Linux commands via Crontab scheduling

I have an EC2 AWS instance where various commands and scripts work perfectly, but do not execute within a crontab. The Crontab contains: 15 03 * * * pythonscript1.py 45 03 * * * pythonscript2.py 00 05 * * * gsjson 1z5OlqsyU5N2ze1JYHJssfe1LpKNvsr4j8TDGnvyu ...

Using Vue.js: Passing an object from data() to a mounted() function

I'm facing an issue while attempting to pass the grid array to the createGridChart. An error message stating "grid is not defined" keeps popping up: “grid is not defined”. export default { data() { return { grid: [], } ...

Changing from Basic to JavaScript?

Good evening, I am looking to convert this example code from Basic to JavaScript. Since JavaScript does not support the "Go To" command, I would appreciate it if you could help me with the translation. Thank you in advance. 10 x = 1666.66 20 y = 1.007897 ...

Using TypeORM to Retrieve Data from Many-to-Many Relationships with Special Attributes

Hey there, I'm diving into the world of TypeORM and could really use some guidance. I've been attempting to set up many-to-many relationships with custom properties following the instructions provided here However, I've run into a few iss ...

``Emerging Challenge in React: Ensuring Responsive Design with Fixed Positioning

I'm currently encountering a challenge with my React application. I've developed a website using React that includes a component named CartMenu, which is integrated within another component called Products. The issue arises when I utilize the de ...

Utilizing jQuery to remove a class with an Ajax request

My setup includes two cards, one for entering a postcode and another with radio buttons to select student status (initially hidden). An Ajax request validates the postcode input - turning the card green if valid (card--success) and revealing the student se ...

Sorting a multidimensional array by keys encounters issues when faced with duplicate keys

One of my functions, sortBy(), is designed to sort multidimensional arrays by a specific key. Take a look at this example array: Array ( [0] => Array ( [id] => 4 [type] => 1 [game] => 1 [platform] => 0 ...

Changing a JavaScript array by including a numerical value

Here is my original dataset... [{ month: 'Jan', cat: 'A', val: 20 },{ month: 'Jan', cat: 'B',' val: 5 },{ month: 'Jan', cat: &ap ...

suggesting options comparable to addthis or sharethis

Could you please check out ? There is a sharing box in the bottom right corner that resembles ShareThis. However, as far as I know, ShareThis does not have options for embedding or submitting content. Does anyone happen to know which plugin is being used ...

Having difficulty with collapsing Bootstrap side navigation menu levels

I've been searching high and low for an example that matches my current issue, but so far, no luck. I'm attempting to design a collapsible side navigation with multiple levels in bootstrap using bootstrap.js. The problem I'm facing is that ...

Combining the values of two arrays into a single table using PHP

My task involves working with two arrays: $aGente = array('jan'=> 'm', 'alice'=> 'v', 'veronica'=> 'v', 'herman'=> 'm', 'maria'=> 'v', &apos ...

Error: React cannot render objects as children

I am encountering an error that I cannot seem to figure out. The issue seems to be with the following line of code: <p className="bold blue padding-left-30">{question}</p> Specifically, it does not like the usage of {question} in the above pa ...

What is the best way to link a generated PHP-AJAX link with a jQuery action?

Imagine a scenario like this: trollindex.htm: [...] <script> $(document).ready(function(){ $("* a.jquery").on("click",function(){ $.ajax({ type: "POST", url: "trollcommander.php", data: ({comman ...

Utilizing JavaScript to dynamically reference object IDs in Yii

Currently, I am in the process of creating a tabular input using Yii and everything is functioning correctly. I can save values for all fields without any issues. However, I have an additional requirement - I want to display a field next to each input that ...

Nested loops in JavaScript can be combined with promises to efficiently handle

I am facing a challenge in looping through an array that contains another array as one of the parameters. My goal is to iterate through this nested array according to specific requirements, and then execute a function once the parent loop is finished. Can ...