Using Javascript to segregate JSON object attributes into a fresh array

I have over 100 JSON objects in this specific format:

{
"grants":[
{
    "grant":0,
    "ID": "EP/E027261/1",
    "Title": "Semiconductor Research at the Materials-Device Interface",
    "PIID": "6674",
    "Scheme": "Platform Grants",
    "StartDate": "01/05/2007",
    "EndDate": "31/10/2012",
    "Value": "800579"
}, ... more grants

My goal is to extract the EndDate and Value properties into a new array as shown below.

"extractedGrants":[
{
    "EndDate": "31/10/2012",
    "Value": "800579"
}, ... more extracted objects with EndDate and Value properties.

I am attempting to achieve this using array.map(function (a) {}); but I'm struggling with the code inside the function.

Any help would be greatly appreciated.

Answer №1

To achieve the desired outcome, consider destructuring the object and creating a new object for the resultant array.

var obj = { grants: [{ grant: 0, ID: "EP/E027261/1", Title: "Semiconductor Research at the Materials-Device Interface", PIID: "6674", Scheme: "Platform Grants", StartDate: "01/05/2007", EndDate: "31/10/2012", Value: "800579" }] },
    result = obj.grants.map(({ EndDate, Value }) => ({ EndDate, Value }));

console.log(result);

ES5

var obj = { grants: [{ grant: 0, ID: "EP/E027261/1", Title: "Semiconductor Research at the Materials-Device Interface", PIID: "6674", Scheme: "Platform Grants", StartDate: "01/05/2007", EndDate: "31/10/2012", Value: "800579" }] },
    result = obj.grants.map(function (a) {
        return { EndDate: a.EndDate, Value: a.Value };
    });

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 action is undefined and cannot be read for the property type

Using the React+Redux framework, I encountered an error: https://i.sstatic.net/0yqjl.png During debugging, the server data successfully reached the state, but the action was empty: https://i.sstatic.net/41VgJ.png Highlighted below is a snippet of my co ...

Converting JSON data into a jQuery-powered spreadsheet

Recently, I completed a module on data visualization, where I learned how to transform Google Spreadsheets into JSON using jQuery. In my spreadsheet, there are two simple columns: date and status (collected data about myself for practice in visualizing it ...

How can I use JavaScript to capture the "MDCMenu:selected" event for the Menu Component in Material Design Web Components?

I am currently using the Material Design Web Components plugin known as "Menu." Once the menu is launched, my goal is to be able to effectively handle all possible ways a "menu item" can be selected. While adding an on-click event through my framework&apo ...

Creating a dynamic calendar text box with JavaScript: a step-by-step guide

I am trying to dynamically add a calendar text box on each click of a link. While I have tested some codes, it seems that the functionality only works for the first declaration and not for subsequent ones. Here's the code snippet I have been working ...

How to retrieve nested menu items within the scope by utilizing JSON and AngularJS

I have recently started working with angular and am facing difficulty in accessing the submenu items within my angular application. While I can successfully access the top level menu items, I am struggling to retrieve the second level items. Here is a sni ...

Converting JAX-RS/JAXB JSON to POJO will only map fields that exist in both the JSON and the POJO, ignoring any additional fields present

In the process of developing an API that must support JSON payloads for PUT/POST requests, which may contain additional fields beyond what is defined in the known POJO. For instance: @XmlRootElement public FruitCounter { int numberOfApples; int num ...

The synchronization and network bandwidth optimization of Angular and Firebase's three-way binding system

Is it possible to synchronize the text box content across various clients using Angular and Firebase? I am curious to know if Firebase will update the database with each letter I type. How does this process impact network bandwidth? Can you explain how it ...

The scaling of the slick carousel image is not working as expected

Having an issue with the Slick carousel where images are not resizing based on browser size (original size is 300x228px). Just to clarify: When I shrink the browser window, the number of slides decreases but the images remain the same size. I've bee ...

Guide on how to optimize an ajax call in the browser to prefetch JSON data

One scenario I am dealing with involves initiating multiple ajax calls upon page load. The first call fetches data in JSON format from the server, and subsequent user interactions trigger further ajax requests that require database queries to process the J ...

What is the best way to export a variable from a p5.js file and then import it into THREE.js?

I am working on a project that involves creating a perlin noise Canva using p5.js and then importing it as a texture into a THREE.js file. However, I am encountering an error where the exported variable is not defined. In my p5.js script, I tried using "e ...

In jQuery, conditionally nest divs within another div based on a specific requirement

There is a container with multiple nested elements that need to be rearranged based on the value of their custom attribute. The goal is to reorder those elements at the end of the container if their 'data-keep-down' attribute is set to true, usin ...

Why do the values of rows and columns suddenly turn into unforeseen numbers once I modify the input assigned to them?

Greetings! I have been working on a simulation of a forest fire spreading, and I am almost done with it. However, I encountered an issue when I tried switching the numbers of rows and columns - suddenly they became huge numbers for no apparent reason. I kn ...

Removing a JSON file using PHP

Currently, I am in the process of writing a json-file within a newly generated folder. My goal is to automatically delete this folder along with its contents after an hour has passed. Here's what I have attempted: $dir = "../tmpDir"; $cdir = scan ...

The method's title does not correspond to a function

I am having difficulty calling the method within my module. An error occurred: TypeError: usr.User.getAddress is not a function I am unsure of how to resolve this issue, as I suspect there may be a problem in my module code. My goal is to retrieve the ad ...

The Camera component in React Native is not currently supporting foreground service

Trying to capture images in a foreground service, such as taking pictures while the user is using another app. Everything works fine within the app, but when the app is closed with the foreground service active, the camera stops working and shows this erro ...

Using jQuery to retrieve values from clicked buttons

I'm trying to retrieve the values of a jQuery button upon form submission, but my current setup is not working. Specifically, I need to extract the value of data-url. Below is the code snippet I am using: $("#addAgency").submit(function(event) { ...

How can I efficiently transfer information between AngularJS modules?

Angular offers the flexibility of creating independent Modules that can be reused in various parts of your application. Imagine having a module dedicated to managing lists, which you want to use across your entire application and populate in different ways ...

AngularJS Controller assigns unexpected value to object

While working on a project, I encountered an issue with assigning float values fetched from a REST API using Angular. Instead of correctly assigning the float value, it was being set as 0. To illustrate my problem clearly, let's consider the followin ...

How to center a div vertically in a window

I have a CSS layout that positions a div both horizontally and vertically relative to the window. It works well, but when I scroll down the page, the div stays centered as if the page hadn't moved. width:600px; height:300px; position:absolute; left:5 ...

What is the best way to cancel Interval in a React application?

I need help with implementing setInterval in my react redux application. Below is the code snippet from FileAction.js export const SetPath = ({ path, location }) => async (dispatch) => { try { let interval; if (pre_path === path) ...