Turning JSON into an array without needing to use a key can be achieved by simply

I'm facing a challenge with an array of objects in the following structure:

[{
    "X": "valueX",
    "Y": "valueY",
    "Z": "valueZ"
}, {
    "X": "anotherX",
    "Y": "anotherY",
    "Z": "anotherZ"
}]

My goal is to transform this data into a two-dimensional array as shown here:

[["thisA","thisB","thisC"], ["thatA","thatB","thatC"]]

I am aware that we can achieve this using the map() function while specifying the keys (X, Y, Z).

newArray = dataArray.map(item => [item['X'], item['Y'], item['Z']])

However, I am looking for a more generic solution that would work regardless of the specific keys used in the array. Since the content and keys of the array may vary, is there a universal method to accomplish this transformation?

Answer №1

let array = [{
  "X": "thisX",
  "Y": "thisY",
  "Z": "thisZ"
}, {
  "X": "thatX",
  "Y": "thatY",
  "Z": "thatZ"
}]

const output = array.map(Object.values)

console.log(output);

Answer №2

While punksta's solution is elegant, my approach to achieving the same result in automatic mode would be as follows:

const src = [{
    "A": "thisA",
    "B": "thisB",
    "C": "thisC"
}, {
    "A": "thatA",
    "B": "thatB",
    "C": "thatC"
}]

const result = src.map(o => Object.keys(o).map(k => o[k]))

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

The range slider's color value is not resetting correctly when using the <input>

Before repositioning, the slider is at this location: (both thumb and color are correctly positioned) https://i.stack.imgur.com/J3EGX.png Prior to Reset: Html <input id="xyzslider" class="mdl-slider mdl-js-slider" type="range" min="0.0" max="1.0" va ...

How to control Formik inputs from an external source

I'm currently developing an application with speech-to-text commands functionality. I have created a form, but I am looking to manipulate the input elements from outside the form framework. <form id="myform"> <input type="tex ...

What steps can be taken to send the user to the login page after their session token has expired

Currently, I am using a Marionette + Node application. I have noticed that when the token expires, the application does not respond and the user is not redirected to the LogIn page. My question is, how can I set up a listener to check the session token s ...

Basic setup of an embedded database in MongoDB

I am currently facing a dilemma as I try to establish the database structure for my Mongodb application using Mongoose/Node.js. I have two main entities, namely Users and Books, and due to the lack of joins in MongoDB, I need to decide on an embedded syste ...

Using HTML5 data attributes as alternative configuration options in a jQuery plugin can present challenges

I am currently in the process of creating my very first jQuery plugin, and I have encountered a challenge when attempting to extend the plugin to support HTML5 data attributes. The idea is for a user to be able to initialize and adjust settings simply by u ...

Accessing an API to retrieve a JSON array populated with various objects

I have been attempting to access data from an API with a specific structure: [ { "bikeID": 5, "manuid": 168, "name": "Gran", "StoreID": 2 } ] My approach involved using the correct URL ...

Extract a section of the table

I'm looking to copy an HTML table to the clipboard, but I only want to include the rows and not the header row. Here is the structure of the table: <table style="width:100%" #table> <tr> <th class="border"></th> ...

Guide to sending a post request with parameters in nuxt.js

I am trying to fetch data using the fetch method in Nuxt/Axios to send a post request and retrieve specific category information: async fetch() { const res = await this.$axios.post( `https://example.com/art-admin/public/api/get_single_cat_data_an ...

Is it feasible to transmit an image file to PHP using AJAX?

I'm attempting to utilize AJAX to send an image file along with other text information. However, I am encountering difficulties with the file part and am struggling to comprehend how to resolve it. Below is the JavaScript code snippet: //The user ...

Troubleshooting: AngularJS $uibModal Issue Not Resolved

Could you help me troubleshoot my code issue? I can see the initial HTML page, but clicking on "Open" does not trigger any action. There are no errors logged in the console or any other changes observed. app.js var app = angular.module('carApp' ...

Using the array.prototype.map method on props in React.js results in an array that is devoid

Recently, I've started exploring the world of React and encountered a problem while attempting to convert the value of props into a JSX element using array.prototype.map(). You can learn more about this method at this link. Here is a snippet of a Rea ...

Why is my update with upsert: true not working in Express and Mongoose?

var logs = [{ mobilenumber: '1', ref: 3, points: 1000, ctype: 'mycredit', entry: 'sdfsdf', entry: 0 }, { mobilenumber: '1', ref: 6, points: 2000, ctype: 'mycredit', ...

WebDriver encounters difficulty clicking on a certificate error popup window

Currently, I am using webdriver 2.40.0 in C# to interact with my company's website. The issue arises when I encounter a certificate error page while trying to access certain elements. Specifically, after clicking the override link and entering some in ...

How can I create a script for a sliding/toggling menu?

Not sure if it's appropriate to ask, but I'm currently in search of a slide/toggle menu. Despite my efforts on Google, I haven't been able to find exactly what I need. As someone who is more skilled in HTML/CSS than jQuery or other scripting ...

Using styled components but lacking the ability to use the className property

Currently, I am working with React and styled components and utilizing a third-party component called IntlTelInput. Below is a snippet of my code: const StyledIntlTelInput = styled(IntlTelInput)` background: blue; `; export default function PhoneNu ...

Initiating Ajax to trigger the body's onLoad event

Whenever I use an ajax call to load a div, the entire page refreshes. It seems that the 'body onload=init()' event is triggered on ajax response causing all the initialization to repeat, which is not desired. Is there a way to only load the div t ...

Why does a React error keep popping up when trying to set a background-image in my CSS?

I've been working on my React project and I can't figure out why I keep encountering this error. I double-checked the URL paths and made sure they were named correctly, yet the error persists. Here is a snippet of my CSS: background-image: url ...

Problem with Bootstrap slider animation

Currently, I am tackling a bootstrap slider project, where the images are all in place, and they transition every few seconds. Everything appears to be functioning correctly, except for the animation display. The image changes suddenly without any smooth t ...

Arranging objects in an NSMutableArray in a specific order

Seeking assistance from knowledgeable individuals. I am currently working on populating a NSMutableArray with multiple objects and I require assistance in sorting the order based on the first element, which will invariably be a number. Any advice on how ...

Angular Material Password Confirmation

Currently, I am working on implementing user authentication using Angular Material. Specifically, I am focusing on displaying the appropriate error message when the confirmed password does not match the initially entered password. Below is the snippet of ...