Utilize only certain JSON properties within JavaScript

I have access to an array of JSON objects.

[
{
Id: "1",
StartNo: "1",
ChipNo: "0",
CategoryId: "0",
Wave: "0",
Club: "",
FirstName: "Lotta",
LastName: "Svenström",
FullName: "Lotta Svenström",
ZipCode: "24231"
},
{...}
]

My goal is to create a new data structure that only includes the StartNo and FullName properties. How can I accomplish this?

I attempted the following:

$scope.runners = [];

for(var i = 0; i<data.length; i++){
    $scope.runners[i].StartNo = data[i].StartNo;
    $scope.runners[i].Fullname = data[i].Fullname;
}

However, this approach is not successful in achieving my desired outcome.

Answer №1

To create a fresh object with only two specific properties, follow this code snippet:

for (let i = 0; i < $scope.runners.length; i++){
    $scope.runners[i] = {
        StartNo: data[i].StartNo,
        Fullname: data[i].Fullname
    }
}

If you want to maintain references and remove all other properties from the object, consider using this approach:

for (let i = 0; i < $scope.runners.length; i++){
    for (let k in $scope.runners[i]) {
        if ($scope.runners[i].hasOwnProperty(k) && k !== 'StartNo' && k !== 'Fullname') {
            delete($scope.runners[i][k]);
        }
    }
}

Answer №2

To transform your JSON data, utilize the map function provided by the Array object:

var jsonData = [...your JSON objects...];

$scope.players = jsonData.map(function(player) {
    return {
        PlayerID: player.PlayerID,
        Name: player.Name
    };
});

Answer №3

To achieve this, simply utilize the built-in map function as shown below:

const $results = data.map(function(item) {
                     return {ID:item.ID, Name: item.Name};
                     });

Answer №4

give this code a shot

    var info=[
{
Id: "1",
StartNo: "1",
ChipNo: "0",
CategoryId: "0",
Wave: "0",
Club: "",
FirstName: "Lotta",
LastName: "Svenström",
FullName: "Lotta Svenström",
ZipCode: "24231"
}
];

var participants =[];


for(var x = 0; x<info.length; x++){
participants[x]={};
   participants[x].StartNo = info[x].StartNo;
    participants[x].Fullname = info[x].Fullname;
}

Answer №5

 $scope.runners=[];
$scope.sampleRunner = [
    {
        Id: "5",
        StartNo: "7",
        ChipNo: "2",
        CategoryId: "4",
        Wave: "3",
        Club: "",
        FirstName: "Ella",
        LastName: "Johnson",
        FullName: "Ella Johnson",
        ZipCode: "55421"
    },
    {
        Id: "6",
        StartNo: "8",
        ChipNo: "3",
        CategoryId: "3",
        Wave: "2",
        Club: "",
        FirstName: "David",
        LastName: "Smith",
        FullName: "Dave Smith",
        ZipCode: "12345"
    },];

    angular.forEach($scope.sampleRunner, function (data) {
            $scope.runners.push({ StartNo: data.StartNo, FullName: data.FullName });
        });

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

Launching a modal using a method in Vue.js that includes constantly changing content

I am currently developing a feature in my VueJs component that involves opening a modal when a certain condition becomes true, and then retrieving data from a controller to display in the modal. After searching online, I have not been able to find clear i ...

Conceal the parent component's <button> element within the Child component

I'm facing an issue with hiding a button in my parent component from the child component. I tried using props to bind the element and v-show directive to hide it, but instead of just hiding the button, it ends up hiding the entire tab. Take a look at ...

Activate or deactivate a button depending on the input value of a TextField in React.js

I am currently designing a form similar to the one displayed in this image. I want the "Add" button to become active as soon as the user starts typing in the "Tags" input field. For this project, I am using material-ui and I have created an Input compone ...

Encountered an issue while executing the next build process

Every time I run npm next build, it throws an error message. Despite my efforts to search for a solution online and installing the "extract-text-webpack-plugin," the issue persists. The error being thrown is as follows: > next build (node:8136) Depre ...

Dynamic Visualizations with D3.js: Live Charts for Real-Time

This is my first time using D3.js and I am attempting to create a basic sparkline graph. The graph should have time on the X-axis and numbers up to 1000 on the Y-axis. I have a web socket server that sends random numbers up to 1000 to clients. My goal is t ...

The JSON.parse function encountered a ReferenceError because the parse method was not properly defined

I am encountering the error message "ReferenceError: parse is not defined" when executing the following line in Node V6.11.0 within an express router. router.post('/api/addComp', function(req,res) { var tempData = JSON.parse('{"compName" ...

Leverage the model attribute in a Spring controller to conditionally display elements using

I am currently working with spring boot, angularjs and using html as the view. This setup is being implemented in index.html. <a ng-show="permission=='write'"> By returning the 'permission' value in the model from the spring boo ...

How can information be exchanged between PHP and JavaScript?

I am working on a concept to display a graph of someone's scores within a div element. The process involves a button that triggers the graph drawing function upon clicking. Additionally, there is a data retrieval function that retrieves information fr ...

Is there a way to convert NuGet.NuGetVersion to a class in C# through deserialization?

As a novice programmer working on an application for my job, I recently encountered an issue while trying to deserialize a class. The class in question is named Nuget, and its structure is as follows: public class Nuget { public string? Name { get; set ...

Is there a specific type of input that can cause an error when using PHP's json_encode function?

Every time I try to pass an unconventional $var to json_encode($var), it never seems to fail. It's able to deserialize some Objects in mysterious ways. I'm searching for a scenario where PHP encounters something that is not JSON serializable (if ...

Loading a Vue.js template dynamically post fetching data from Firebase storage

Currently, I am facing an issue with retrieving links for PDFs from my Firebase storage and binding them to specific lists. The problem arises because the template is loaded before the links are fetched, resulting in the href attribute of the list remainin ...

AJAX responses sans the use of jQuery

Similar Question: How can I achieve this through AJAX? After my previous attempt at asking this question, where I was not clear enough, I am making another effort to be as specific as possible. To start with, I have my data encoded using the json_enc ...

Utilizing Node.js for Lambda Functions

I am attempting to retrieve a list of all EC2 instances in a specific region using a lambda function. Below is the code snippet I am using: const AWS = require('aws-sdk'); AWS.config.region = '******'; exports.handler = async function( ...

Leverage the JSON Input step for handling irregular data structures

Trying to process the following JSON data using a JSON Input step: {"address":[ {"AddressId":"1_1","Street":"A Street"}, {"AddressId":"1_101","Street":"Another Street"}, {"AddressId":"1_102","Street":"One more street", "Locality":"Buenos Aires"}, ...

Preserving the raw JSON data within deserialized JSON.NET entities

This is a continuation from a previous query concerning Newtonsoft Object → Get JSON string . Here is a representation of the object I am working with: [JsonConverter(typeof(MessageConverter))] public class Message { public Message(string original) ...

Failing to verify the presence of specific text within a dropdown menu using Selenium

Previously, I successfully implemented this code, however, the HTML/CSS for the dropdown has since changed and now I am unable to get it to function correctly. Below is the structure for the dropdown code, with specific text highlighted that I am trying t ...

What is the process of building a service in AngularJS?

I have written this code using a service in angular.js. Upon running the code, I encountered an error Uncaught Error: [ng:areq]. </ons-toolbar> <div ng-controller="testAppController"> Search: <input ng-model="query" type=" ...

Testing a component in Angular 2 that utilizes the router-outlet functionality

I recently set up an angular 2 project using angular-cli. As part of the setup, I created a separate AppRoutingModule that exports RouterModule and added it to the imports array in AppModule. Additionally, I have the appComponent which was generated by an ...

Prevent the selection of rows and only enable the option to select checkboxes in PrimeNG's Multiselect feature

I'm currently working on a task that involves using PrimeNG multiselect. This particular multiselect includes checkboxes followed by text within each row. I need to disable the ability to select rows by clicking on the text, and instead only allow sel ...

Error: The login is invalid due to authentication failure with code 535 5.0.0

I'm currently working on setting up Outlook calendar events by integrating a mailing service with my project. I'm using the Express framework and Mongoose queries for this purpose. Below is the code snippet I have implemented: var _ = require(& ...