What is the most optimal method for transforming this array of objects into a different format?

My array consists of objects structured like this:

[
{prop1: valueA, prop2: valueB, prop3: valueC},
{prop1: valueD, prop2: valueE, prop3: valueF},
...
]

I am looking to transform this array into objects with a different structure:

[
{x: valueA, y: valueB},
{x: valueD, y: valueE},
...
]

In order to achieve this transformation, I need to select specific properties from the original objects and also rename them. Would using the rest operator be the best approach for this task? If so, what would be the most effective way to implement it? Your insights are much appreciated.

Answer №1

Transforming an array using Array.map seems like the best approach for this task. Is there anything else you had in mind?

let results = [
{prop1: 'valueA', prop2: 'valueB', prop3: 'valueC'},
{prop1: 'valueD', prop2: 'valueE', prop3: 'valueF'},
].map(item=>({x:item.prop1, y: item.prop2}));

console.log(results);

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 use of jQuery.parseJSON is ineffective for a specific string

Why isn't jQuery.parseJSON working on this specific string? ({"stat":"OK","code":400,"data":[{"title":"Development Convention","event_type":false,"dates_and_times":[{"date":"28\/03\/2012","start_time":"10:00 AM","end_time":"10:00 AM"},{"dat ...

Android response JSON scrambled

The server's response looks something like this: { "data": { "table": { "0": { "pay_date": "2017-04-28", "status": "Paid", "bill_date": "2017-04-27", }, "1": { "pay_date": "2 ...

ChartJS v2: Displaying scale value based on click coordinates for time scale

I am having an issue with a time-based line chart where I am trying to retrieve the values for each scale at the click coordinates. In my ChartJS options, I have defined an onClick function: onClick: function(event, elementsAtEvent) { console.log(eve ...

Creating a dynamic JSTree that loads data on demand using Stored Procedures

Currently in my SQL Server database, I have two Stored Procedures that are responsible for extracting data from a tree structure: One procedure retrieves all nodes at a specific level based on the provided level number. The other procedure retrieves th ...

My local requests are being blocked by both Chrome and Firefox

Recently, I've been experimenting with local flash development and trying to inject my swf file into a website served by my test server. To enable loading of local resources in Chrome, I set --disable-web-security. In FireFox, I made the following a ...

Combine numerous JSON files into a single JSON file

I have numerous JSON files that look like this: For example: 1.json {"name": "one", "description": "testDescription...", "comment": ""} test.json {"name": "test", "description": "testDescription...", "comment": ""} two.json {"name": "two", "descript ...

The world of coding comes alive with Quartz Composer JavaScript

Seeking assistance as I navigate through a challenging project. Any help would be greatly appreciated. Thanks in advance! I have a Quartz Composition where I aim to incorporate a Streetview from Google Maps and control the navigation, but I'm unsure ...

Can you share tips for passing a variable from a post request to a function that accepts parameters as a string or an array of strings in Node.js?

I am struggling to insert the variable named query into the end of the prompt. I attempted to use template literals but it was unsuccessful. (async () => { const gbtResponse = await openai.createCompletion({ model: "text-davinci-002", prompt ...

Error thrown when attempting to access properties of null values (Uncaught TypeError: Cannot read properties of undefined (reading 'map'))

import React, { useState, useEffect } from "react"; import { TaskLists } from "./TaskLists"; import { Daycard } from "./daycard"; import { getTasks, deleteTask } from "../api/task.api"; export function TaskManager() { const [tasks, setTasks] = useState( ...

Clicking on a column or x-axis category in Highcharts will automatically include the job number in the page URL

Check out the code I've put together: http://jsfiddle.net/a9QwS/ I'm looking to add functionality where clicking on a column or x-axis label will append its data to the URL. For example, in PHP, I can do something like this: echo " <td sty ...

Accessing the jQuery Ajax success variable

I have a PHP function that returns an array with an element error containing the value 'ERR': var updatePaymentType = function(plan_pt_id, pt_id){ var error = null; var data = new Object() data["function"] = "update"; ...

Is the create attachment function of the Microsoft Graph API not functioning properly?

I've been trying to create an attachment for my messages by following the documentation provided, but unfortunately, the API seems to be giving me some trouble. I referred to the document at for guidance. Below is the JavaScript code that I have bee ...

Why does the request for server parameter 'occupation=01%02' function correctly, while 'occupation=01%2C02' or 'occupation=01&occupation=02' result in an error?

There seems to be an issue with the parameter when using the API to request data from the server. The value 'occupation=01%02' works correctly when entered directly into the browser URL, but errors occur when using 'occupation=01%2C02' ...

Reverting HTML entities in Json to their original characters

Issue and initial data In my json dataset, there are HTML entities used to encode special French characters like “é”, “ç”, “à”, etc., as well as html tags. Here is a snippet of the json data: { "data1": "&lt;p&gt; ...

implement adding a division element to the DOM using the append

I am facing an issue with this particular code. The intended functionality is to create multiple divs in a loop, but it seems to be dysfunctional at the moment. Upon clicking, only one div appears and subsequent clicks don't trigger any response. < ...

Implementing an API call using Redux Saga within a React application

I have been diving into learning saga by following a tutorial on Medium and trying to implement it in StackBlitz. I encountered a path issue initially, managed to fix it after several attempts, but now a different problem has come up. Despite searching on ...

Issue with ngTable: Error retrieving data for server-side pagination

I am currently working on setting up a server-side table using ng-table. However, I am encountering some issues with the getData function. It keeps giving me errors such as $defer.resolve is not a function or params is not defined. I noticed that I can ac ...

Python 3.6 encountering issues with converting string to json format

My challenge involves converting the string below to JSON using json.loads(): targetingConditions = "[{\"filters\":[{\"key\":\"domain\",\"rel\":\"neq\",\"values\":['science.howstuffworks.com ...

What is the process of utilizing marked plugins within a Vue3 project?

I attempted to integrate the marked plugin into my Vue.js applications. After installing [email protected], I did not encounter any issues during compilation. However, when I viewed the contents in the browser, nothing appeared. My Vue project was built u ...

Enhanced data visualization with Material UI's nested datagrid feature

Is there a way to display nested JSON data on a React Material UI data grid? I'm looking to showcase the phone numbers of users from the JSON in the provided sandbox example. You can check out the demo here. ...