Transforming an Array Object into an Array List

What is the most efficient way to transform this array object?

a = [
  {"id" : 1, "name": "a"},
  {"id" : 2, "name": "b"},
  {"id" : 3, "name": "c"}
]

into:

b = [
   [1, "a"],
   [2, "b"], 
   [3, "c"]
]

Answer №1

const newArr = arr.map((item) => [item.id, item.name]);

Answer №2

Using the map function in combination with Object.values

const newArray = array.map(item => Object.values(item));

Answer №3

map each item utilizing Object.values.

const  x = [
 {"id" : 1, "name": "apple"},
 {"id" : 2, "name": "banana"},
 {"id" : 3, "name": "cherry"},
]
const y = x.map(Object.values);
console.log(y);

Answer №4

Utilizing the map function

arr = [ {"id" : 1, "name": "apple"}, {"id" : 2, "name": "banana"}, {"id" : 3, "name": "carrot"} ]
result=arr.map(obj=>[obj.id,obj.name])
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

JavaScript: Collecting data into an array of objects

Is there a more efficient way to fetch a specific object from an array of objects without using a for loop? Here is an example of the array of objects: 0: {code: "YJYDWO1", route_id: 1} 1: {code: "YJYDWO2", route_id: 2} 2: {code: "YJYDWO3", route_id: 3} 3 ...

Adding wrapAll in jQuery or PHP after tags with identical IDs can be achieved by selecting all the target elements

web development <?php foreach ($forlop as $value) : ?> <?php $stringTitle = substr($value->getTitle(), 0, 1); ?> <?php if(is_numeric($stringTitle)){ echo "<h3 id ...

Troubleshooting why content set to a <div> element with JavaScript / jQuery is not persisting after a

This is the current code snippet I am working with: <asp:Button ID="btnSave" runat="server" OnClick="Save" CssClass="StylizedButton" resourcekey="btnSave" /> <div id="lbltot"></div> Below is the JavaScript portion of the code: $(do ...

Do I need to include the title, html, and head tags in a page that is being requested via ajax

I have a page called welcome.htm that is being loaded into another page using ajax. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xh ...

Is it better to use $("#myElement").length or !$("myElement") for checking the existence of the DOM element?

Is it necessary to use the length property when checking if a specific DOM element exists or not? I have seen suggestions from other posts recommending the use of the length property for this purpose, and it seems to work as expected. However, I have also ...

What is the best way to include a JavaScript function in a dynamically generated div?

By clicking a button, I am able to dynamically generate a table row that contains a div element with the class "contents." $(document).on("click", ".vote", function() { // Removing selected row var rowLoc = this.parentNode.parentNode. ...

Trigger click functions sequentially to display elements after specific actions are taken

Is there a way to make jQuery listen for clicks only at specific times? It seems that when I add event listeners, like $("#box").click(function(){, they are executing before the code above them finishes running. Let's say I have two boxes that should ...

Pressing a button will reset the input spinner in Bootstrap

Is there a way to reset the bootstrap input spinner by clicking a button? I attempted using document.getelementbyId().value = "0" and also tried using reset(), but neither method worked. Any suggestions on how to successfully reset it? function resetScor ...

The glTF file lacks visible content

My goal is to showcase a glTF file using Three.js. To bypass CORS policy, I have opted for a local server called Servez. While everything runs smoothly on Mozilla Firefox without errors, the content does not appear. However, when attempting the same on Chr ...

Expanding the Data in a Meteor Cursor

Is there a way to loop through a cursor and insert a new field into every document? I tried the following code, but it doesn't seem to be working: function addNewField() { var items = Items.find(); items.forEach(function(item) { item. ...

Including material-ui/core/Table produces a data error before it has even been connected

My Initial Redux Saga Setup In my Redux Saga code, I have a generator function that fetches data from a mock API: function* fetchPickingScans({orderReference}){ try{ const response = yield call( scanningMockApi.getPickingScans, orderReference ...

Selecting Departure and Return Dates using jQuery UI Datepicker

I'm working on creating two DatePicker fields for selecting departing and returning dates. For instance, if a user is planning a 5-night holiday, the initial dates displayed would be: 01/12/2010 | Calendar Icon 07/12/2010 | Calendar Icon With the de ...

Integrating React js with Layout.cshtml: Mastering the Fusion of React Routing and ASP.NET MVC Routing

My current project involves an ASP.NET MVC core application with the View written in cshtml. The routing follows the conventional asp.net mvc routing pattern. However, I've recently implemented a new module using React JS. Now, I'm faced with the ...

Steps for accessing a file using HTML form input data:

I have a unique setup with two distinct fields featuring drop-down lists - one for selecting a User and the other for choosing Data. Additionally, I've included a button that is intended to serve as an output based on the selected options from the dro ...

Creating a Java-based lottery machine that ensures each number is only chosen once for a program to run smoothly

I am in the process of creating a lottery machine using Java. Below is the code I have written: import java.util.Scanner; import java.util.Random; import java.util.Random; public class wissam { public static void main(String[] args) { int ...

AngularJS: Assigning a value to an element

I am facing the challenge of automating an iframe using Selenium Webdriver and need to input a value into a text box. Here is the HTML code: <input class="ng-pristine ng-empty ng-invalid ng-invalid-required ng-valid-maxlength ng-touched" id="name" typ ...

Selenium: Perform a click action on a button enclosed within a "<div><a></a></div>" tag

I attempted to click on a button and encountered this structure: https://i.sstatic.net/GyGk3.jpg <div class="button-wrapper" id="button-verify-wrapper"> <a x-ng-click="verifySfdcConnection()" class="clearfix float-left button-green"> ...

Comparison between forming JavaScript objects using arrow functions and function expressions

What is the reason behind const Todos = function () { ... } const todos = new Todos(); running smoothly, while const Todos = () => { ... } const todos = new Todos(); results in a TypeError: Todos is not a constructor error? ...

Arranging HTML elements using JavaScript or jQuery

Something seems off. The sorting doesn't work as expected. Check out the JavaScript code below: function sortDescending(a, b) { var date1 = $(a).data('date'); var date2 = $(b).data('date'); return date1 > date2; } ...

Angular: Array in the template re-renders even if its length remains unchanged

Below is the template of a parent component: <ng-container *ngFor="let set of timeSet; index as i"> <time-shift-input *ngIf="enabled" [ngClass]="{ 'mini-times' : miniTimes, 'field field-last&ap ...