Looking to simplify the complex JSON structure by converting it into an array using JavaScript

Being a newcomer to javascript, I am struggling with breaking down a complex structure into an array.

The current structure is as follows:

[
    {
        "Key": "ProducePRINKA0370001",
        "Record": {
            "docType": "Produce",
            "PRODUCEID": "PRINKA0370001"
        }
    },
    {
        "Key": "ProducePRINKA0370038",
        "Record": {
            "docType": "Produce",
            "PRODUCEID": "PRINKA0370038"
        }
    },
    {
        "Key": "ProducePRINKA0370050",
        "Record": {
            "docType": "Produce",
            "PRODUCEID": "PRINKA0370050"
          
        }
    }
]

I need to transform this structure into the desired array format below:

[
    {
        "docType": "Produce",
        "PRODUCEID": "PRINKA0370051"
    },
    
    {
        "docType": "Produce",
        "PRODUCEID": "PRINKA0370038"
    }
]

I have attempted various methods but haven't been successful. Any assistance would be greatly appreciated.

Thank you in advance.

Answer №1

If you want to easily extract specific data from an array, you can utilize the map() function like this -

const dataList = [{
    "Key": "ProducePRINKA0370001",
    "Record": {
      "docType": "Produce",
      "PRODUCEID": "PRINKA0370001"
    }
  },
  {
    "Key": "ProducePRINKA0370038",
    "Record": {
      "docType": "Produce",
      "PRODUCEID": "PRINKA0370038"
    }
  },
  {
    "Key": "ProducePRINKA0370050",
    "Record": {
      "docType": "Produce",
      "PRODUCEID": "PRINKA0370050"

    }
  }
];

const extractedDataList = dataList.map((item) => {
  return item.Record;
})

console.log(extractedDataList)

This code snippet demonstrates how the map() function is used to iterate over an initial array (dataList) and extract only the Record part from each object within the array. Essentially, map() creates a new array by applying a specified function to each element in the original array.

Answer №2

The desired outcome can be achieved by utilizing the map function.

let output = inputData.map((element) => element["Data"]);

console.log(output);

In this scenario, inputData refers to the source array containing the necessary 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

Error encountered when attempting to load files from the same domain due to CORS restrictions

I'm currently developing a website at and I am trying to load the content of an HTML file into an element. I have specified the element as $('.about-us-first-col') and I am loading the content using the code: $('.about-us-first-col&apo ...

Having trouble installing the JSON extension in PHP's docker image

Recently, I made the decision to upgrade my projects from PHP 7.4 to 8.0. Currently, I am in the process of rebuilding all my Docker images to align with this upgrade for consistency. Prior to this upgrade, I was utilizing php:7.4 in my Docker setup, incl ...

Is it possible to transform JSON Array Data from one format to another?

As someone who is new to the software field, I am working with a JSON array of objects: var treeObj = [ { "name": "sriram", "refernce_id": "SAN001", "sponer_id": "SAN000" }, { "name": "neeraja", "r ...

Looking to conduct date comparisons within angular-ui-grid?

I'm currently working on an application that utilizes angular-ui-grid to display form data. One issue I'm facing is how to compare the submission date obtained from an API call with the current date within ui-grid, in order to calculate the numbe ...

Is there internal access to the "access property" within the "data property" in JavaScript?

I have come to understand that there are two types of properties in objects: data properties and accessor properties. It is possible to access the "data property" without using an "accessor property," as shown below: const person = { name: 'Pecan&a ...

Dividing the text by its position value and adding it to a fresh object

I needed to divide the paragraph into sections based on its entityRanges. Here is what the original paragraph looks like: { type: 'paragraph', depth: 1, text: 'Do you have questions or comments and do you wish to contact ABC? P ...

NPM: Handling multiple prehooks with the same name

Is it possible to have multiple prehooks with the same name in my package.json file? For example, having two instances of pretest: "scripts": { "start": "react-scripts start", ... "pretest": "eslin ...

Using TypeScript with Watermelondb

I'm currently developing a React App and I want to implement Watermelondb for Offline Storage, but I'm unsure about using it with TypeScript. I have already set up the database and created Course and Lesson model files from the Watermelondb libra ...

Adjust the HTML content prior to displaying it to prevent any flickering

Is there a way to run code before the page is rendered, such as updating dates or converting text to HTML, without causing flickering when reloading multiple times? How can I ensure the code runs as it's drawn instead of waiting until the end to redra ...

Run JavaScript code whenever the table is modified

I have a dynamic table that loads data asynchronously, and I am looking for a way to trigger a function every time the content of the table changes - whether it's new data being added or modifications to existing data. Is there a method to achieve th ...

Sharing environment variables between a React app and an Express.js server that hosts it as a static site can be achieved by setting

My static site react app is hosted under an express server project in a folder called client/build. The oauth redirect uris point to the express server for token retrieval. The react app redirects users to the oauth endpoint, which is also referenced by th ...

Tips for identifying and logging out a dormant user from the server side using Angular 2 Meteor

I'm currently diving into Angular 2 Meteor and working on a project that requires logging out the user when they close their browser window. I also need them to be redirected to the login page when they reopen the app. After searching online, I could ...

I'm curious if there is an eslint rule specifically designed to identify and flag any unnecessary spaces between a block comment and the function or

Can anyone help me find a solution to prevent the following issue: /** * This is a comment */ function foo() { ... } I need it to be corrected and formatted like this: /** * This is a comment */ function foo() { ... } ...

Using Socket.IO in Node.js to distribute information to every connected client

Currently, I am in the process of developing a WebGL multiplayer game. My approach involves using socket.io and express in node.js to enable multiplayer functionality. However, I am encountering an issue with broadcasting key events. When a user presses a ...

Loading Data into Array - Angular/Ionic

I am currently developing an App and encountering issues with pushing data into an array. It seems that there is a problem in my code. Would you mind taking a look? Error Message Thrown: ionic.bundle.js:25642 TypeError: Cannot read property 'title&ap ...

Steps to create a dynamic <div> that is only visible within a parent <div>

First of all, I want to express my gratitude for welcoming me into the group. I am seeking assistance with an inquiry regarding jQuery animation. The specific animation in question can be seen on the navigation menu items of this template from Template Mon ...

What is the most efficient way to read multiple JSON values from a file or stream in Python without putting in too much effort

Is there a way to efficiently read multiple JSON objects from a file or stream in Python, one at a time? The built-in json.load() method reads until the end of the file and does not allow for reading a single object or iteratively over objects. Ideally, I ...

Unable to execute an Angular 2 application within Visual Studio 2015

I encountered an error while trying to set up an environment on VS 2015 with Angular2. Whenever I run the command "npm start," I receive the following error message. I attempted using "npm cache clean --force" before running "npm start," but the error pers ...

Passing data to server-side PHP script using AJAX technology

Currently, I'm facing an issue with passing variables to a PHP script using onclick. It seems that I am making a mistake somewhere. To demonstrate the problem, let's say I have the following code snippet in my main page: <img src="myImage.jp ...

Transforming a typical JSON file into a parent-child hierarchical JSON structure similar to the one utilized in d3's flare.json file format

My JSON file has a specific structure: { "a": "b", "c": "d", "e": { "f": "g", "h": "i" } } I want to transform it into the following structure: { "name": "Root", "parent": "null", "children": [ { ...