Transform a JSON array containing individual objects into a new JSON array with objects

My array contains objects with nested objects in each element, structured like this:

[
  {
    "person": {
    "name": "John",
    "isActive": true,
    "id": 1
   }
 },
 {
    "person": {
    "name": "Ted",
    "isActive": true,
    "id": 2
   }
 }
]

I want to transform it into a format that eliminates the class name, resulting in:

[
  {
    "Name": "John",
    "IsActive": true,
    "id": 1
  },
  {
    "Name": "Ted",
    "IsActive": true,
    "id": 2
  }
]

This conversion is necessary for me to efficiently load the data into a Kendo UI grid. The transformation process takes place in a Typescript file.

Answer №1

To simplify the task, you can destructure the property called person.

var data = [{ person: { name: "John", isActive: true, id: 1 } }, { person: { name: "Ted", isActive: true, id: 2 } }],
    result = data.map(({ person }) => person);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Answer №2

Utilize the map method to transform an array of objects into a new object with specific keys

let data = [{
    "person": {
      "name": "John",
      "isActive": true,
      "id": 1
    }
  },
  {
    "person": {
      "name": "Ted",
      "isActive": true,
      "id": 2
    }
  }
];


let newData = data.map(function(item) {
  return {
    Name: item.person.name,
    IsActive: item.person.isActive,
    id: item.person.id
  };
});

console.log(newData)

Answer №3

Extremely Basic persons.forEach(person => person.name)

var persons=[
  {
    "person": {
      "name": "Sarah",          
      "isActive": true,
      "id": 1
   }
 },
 {
    "person": {
      "name": "Mike",          
      "isActive": true,
      "id": 2
   }
 }
]

var newPersons=persons.map(person => person)

console.log(newPersons)

Answer №4

To utilize the .map method

const data = [{"person": {"name": "Alice","isActive": false,"id": 3}},{"person": {"name": "Bob","isActive": true,"id": 4}}]
.map(({ person }) => {
  return {
    "Name": person.name,
    "IsActive": person.isActive,
    "ID": person.id
  }
})

console.log(data)

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 Socket.io authorization feature is failing to refresh session information

I am currently working on implementing Socket.IO's authorization function to retrieve session data. However, I have encountered an issue where even after logging out and destroying the session, Socket.IO retains the old session information, which is l ...

Obtain the actual file path from a JSON response

I have set up a local express server to serve a JSON file, but I am facing an issue with displaying image files located in a local folder. The image paths are included in the JSON object that I want to display in my DOM. However, the response I receive inc ...

Discovering whether an image contains a caption with the help of JavaScript

There are various websites that display captions on images in paragraphs, h1 tags, or contained within a div along with the image. I am interested in learning how to determine if an image has an associated caption using JavaScript, especially when the cap ...

Troubleshooting Guide: Issues with Bootstrap 3 Modal Window Implementation

This question is so simple that it's embarrassing. I attempted to copy the code from http://getbootstrap.com/javascript/#modals directly into a basic page setup, but it's not functioning. It seems like I'm making a very silly mistake. Here i ...

Creating a custom dynamic favicon and title in NextJS

Hello there! I am in the process of creating a web constructor. Currently, my application functions as follows: I verify the URL that the user is visiting (such as localhost:3000) I identify their project name within my web constructor (localhost:3000 -&g ...

Implementing automatic selection mode in Kendo MVC grid

Seeking to modify the SelectionMode of a Kendo MVC Grid, I aim to switch from single to multiple using Javascript or JQuery upon checkbox selection, and revert back when the checkbox is unchecked. Is this feasible? Additionally, I am successfully binding a ...

Guide on hiding a div element when clicked outside of it?

How can I create a dropdown feature in Khan Academy where it disappears when clicked outside but stays visible when clicked inside, using JavaScript/CSS/HTML? The current implementation closes the dropdown even on internal clicks. The code includes an even ...

Retrieve a variable from the context and assign it to the Angular scope

I'm having trouble accessing the sourcePath in the controller $scope. This is the code I have: (function() { var sourcePath; sourcePath = 'app/assets/javascripts/shared/controllers/some_controller.coffee'; angular.module("shared"). ...

Is it possible to utilize TypeScript code to dynamically update the JSON filter with variable values?

I am dealing with a JSON filter in which the value for firmwareversion needs to be replaced with a dynamic value. Here's how I've set it up: //JSON filter this.comX200FilterValue = '{ "deviceType": "ComX", "firmwareV ...

Header stabilization on scroll

On my webpage, I have a table header positioned in the middle of the page. However, due to the length of the page, I am looking for a way to make the header stay fixed at the top of the browser as the user scrolls down. My query is: Is there a method to k ...

Trying to toggle between two Angular components within the app component using a pair of buttons

Currently, I am developing an application that requires two buttons to display different nested apps. Unfortunately, I am unable to use angular routing for this particular design. These two buttons will be placed within the app.component. When Button A i ...

Modifying iframe src using click event from a separate component in Angular 10

I am looking to dynamically update the src attribute of an iframe when the menu bar is clicked. The menu bar resides in a separate component and includes a dropdown menu for changing languages. Depending on which language is selected, I want to update the ...

Having difficulty controlling the DOM within an AngularJS template

My website utilizes AngularJS templates, specifically a Single Page Application (SPA). Within one of the templates, I am utilizing a tab control from the AngularUI library named Ui-Tabset. During the rendering process, this control generates a ul element ...

Creating a seamless thread using AngularJS

I am working on a Spring MVC application that utilizes HTML and Angular on the client side. I have a method in my Angular controller that needs to run every 5 seconds. How can I achieve this using Angular? Thank you for your assistance. $scope.inspectio ...

Timed up 10-second countdown with vue.js

<html> <head> <meta charset="utf-8" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" ></script> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> < ...

Create a custom overlay for an image that is centered horizontally and does not have a fixed width

I'm working with this HTML setup: <div class="container"> <img class="image" /> <div class="overlay"> <div class="insides">more content here</div> </div> &l ...

Detecting Unflushed Requests in Jasmine and AngularJS

I'm encountering some issues passing certain tests after implementing $httpBackend.verifyNoOustandingRequest(). Interestingly, excluding this from my afterEach function allows the tests to pass successfully. However, including it causes all tests to ...

A step-by-step guide to displaying an HTML table on your view using AJAX and JSON data

Hey everyone! I'm having some trouble appending a table on my view. The JSON result is sending the data correctly, but when I try to display it on my table, it only shows for a split second and then looks like the page refreshes and shows nothing. I c ...

Make sure to select the checkbox using a protractor only if it hasn't been checked already

I am attempting to retrieve a list of checkboxes using CSS and only click on a checkbox if it is not already selected. I have successfully obtained the list, but I am encountering an issue when trying to validate whether or not the element is selected. Ca ...

Providing the module with the URL

How can I retrieve the URL within a module? module.exports = function(app, Reviews, Anon, User, url){ var url = url; console.log("url", url)// url is undefined how to get url function postHandler(req, res){ } app.post("/individual/"+ u ...