Transformation of an Array of Objects into an Array of Arrays

Is there a way to dynamically convert an Array of objects into an Array of Arrays?

For example:

arrayTest = arrayTest[10 objects inside this array]

Each object in the array has multiple properties that are added dynamically, so the property names are unknown.

I want to be able to convert this Array of objects into Array of Arrays dynamically.

P.S. I can convert it if I know the property name of the object, but I want to do it automatically without knowing the property names.

Example (assuming firstName and lastName are known property names):

var outputData = [];
for(var i = 0; i < inputData.length; i++) {
    var input = inputData[i];
    outputData.push([input.firstName, input.lastName]);
}

Answer №1

Transforms an Array of objects into an Array of Arrays:

let result = data.map( object => Object.values(object) );

Answer №2

Give this a shot:

const result = data.map((item) => {
  return Object.keys(item).sort().map((key) => { 
    return item[key];
  });
});

Answer №3

Implement the for-in iteration method

let resultsArray = [];
for (let key in targetObject) {
    // key represents the property name
    resultsArray.push(targetObject[key]);
}

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

Bringing to life in Vue.js

I am a beginner with Vue.js and I have been attempting to integrate Materialize into my project. I have experimented with various plugins such as vue-materialize (https://github.com/paulpflug/vue-materialize) and vue-material-components (https://www.npmjs. ...

AngularJS is causing the D3.js Bubble chart graph to not display properly

I've been attempting to enhance my Bubble chart graph created with D3.js by incorporating AngularJS, but I'm facing some difficulties. Despite going through this tutorial, nothing seems to be working as expected. Below is the code I have written ...

Converting a string into an array of doubles

I have a challenge where I need to transform a string: Dim tmpTry As String = "10, 20, 30, 40, 50, 52, 20, 20, 10, 35, 3, 8, 47, 7, 2, 5, 55, 8, 0, 0, 6, 55, 0, 2, 12, 0, 0, 21, 14, 0, 3" Into a double array: Dim arrNumOfVisits As Double() = New Double( ...

Leveraging the power of AJAX, PHP, and JavaScript to display numerous status updates throughout an extended operation

It has come to my understanding that when using PHP, it is not feasible to send messages to the DOM using AJAX as the entire script must finish executing before the response becomes available. This leaves me with two possible solutions: Break down the le ...

What is the reason for the value of an object's key becoming undefined when it is set within a loop?

I've always wondered why setting a certain object's key as its own value in a loop results in undefined. Take this code block, for example: var text = 'this is my example text', obj = {}, words = text.split(' '); for (i = ...

Array of Geographical Location Data Provided by Google Maps Geocoding

Utilizing a library for Google Geocoding API Wrappers (https://code.google.com/p/gmaps-api-net/) to retrieve or map a full address may sometimes result in inaccuracies. This is often due to missing address types returned by Google, causing discrepancies in ...

It is imperative that the query data is not undefined. Be certain to provide a value within your query function that is not undefined

I am utilizing a useQuery hook to send a request to a graphql endpoint in my react.js and next.js project. The purpose of this request is to display a list of projects on my website. Upon inspecting the network tab in the Chrome browser, the request appear ...

What steps should be taken to ensure that my nodeJS server can maintain the identity of a specific user?

Currently, I am in the process of building a mobile application that utilizes Flutter for the front-end and NodeJS for the back-end. Progress has been steady, but I have hit a roadblock while trying to incorporate a lottery feature. The idea is for the se ...

What is the best way to dynamically create jest.mock(file)?

Attempting to simulate react-router-dom like this: jest.mock('react-router-dom', () => ({ ...jest.requireActual('react-router-dom'), useLocation: () => ({ pathname: '/random/path', }), })) While this ...

How come I'm encountering issues when trying to click on the "register-selection" button in my Bootstrap and JS setup?

I am facing a challenge while developing my website. I want to trigger an alert when the "register-selection" is clicked, but despite trying both Jquery and vanilla Javascript, I have not been successful. Even after searching online resources and ChatGPT f ...

Leveraging JavaScript for setting an ASP.net Dropdownlist

I have made several attempts to set the dropdownlist without success. Despite observing the selectedIndex changing appropriately in developer tools, and noticing the selected option node change from false to true, the dropdownlist still displays the first ...

I am looking to retrieve a specific input value from a JSON array using JavaScript

I have created an array called 'PROPERTIES' which accepts values like username, password, sid, etc. I am looking to retrieve these entered values using JavaScript. 'PROPERTIES': {'gatewayurl': {'Name': ...

What is the best way to adjust for additional spacing created by a scrollbar using CSS?

Is there a way to maintain alignment between two elements on a webpage, even when a scrollbar disrupts the layout? This challenge arises when one element needs to be aligned with another, but the appearance of a scrollbar throws off the alignment. How can ...

Using Mongoose to delete a document based on the condition of a subdocument value

I'm attempting to delete a document from the "challenges" collection, but only when the user is listed as a participant with the admin role for that challenge. Although my current code logs challenge deleted, the challenge is not actually being remov ...

Animating rows to smoothly glide across the screen

I have created a container with four rows, each containing a different number of columns. When I click on a row, it should move to the next row. However, when I click on the final row (which is in the last position), it should wrap around and move to the ...

What is the reason for this assignment not being activated?

After going through the official xstate tutorial, I decided to create my own state machine inspired by a post on dev.to by a member of the xstate team. Everything is working fine except for the fact that the output is not being updated. It seems like the ...

Utilizing a Single Background Image Across Several Div Elements

Let me illustrate my question using a sequence of images. The div container is set to display the background image as follows: In addition, there will be tile-shaped divs layered on top of the image: I aim to have the background image visible only withi ...

Is there a way to calculate the total sum of the elements within an array using Bash scripting?

Is there a way to sum the elements of an array created using user input with the read -a command? ...

Should I fork and customize npm package: Source or Distribution? How to handle the distribution files?

Currently, I am in the process of developing a VueJS web application. Within this project, there is a module that utilizes a wrapper for a JavaScript library obtained through npm and designed to seamlessly integrate with VueJS. However, it doesn't com ...

Seeking a quick conversion method for transforming x or x[] into x[] in a single line of code

Is there a concise TypeScript one-liner that can replace the arrayOrMemberToArray function below? function arrayOrMemberToArray<T>(input: T | T[]): T[] { if(Arrary.isArray(input)) return input return [input] } Trying to cram this logic into a te ...