Combining two sets of JSON data through mathematical operations

Two JSON files are available: one containing team names and codes, and the other with match details and scores. The goal is to create another JSON file that aggregates total matches won-lost and total goals.

The JSON files are located at:

  • JSON 1
  • JSON 2

I attempted to use the rest operator...

Example JSON Data - Json1:


{
"name": "English Premier League 2011/12",
"clubs": [
{
"key": "chelsea",
"name": "Chelsea",
"code": "CHE"
},
{
"key": "arsenal",
"name": "Arsenal",
"code": "ARS"
},
{
"key": "tottenham",
"name": "Tottenham Hotspur",
"code": "TOT"
}
]
}

Example JSON Data - Json2:


{
"name": "English Premier League 2011/12",
"rounds": [
{
"name": "Round 1",
"matches": [
{
"date": "2012-01-11",
"team1": {
"key": "tottenham",
"name": "Tottenham Hotspur",
"code": "TOT"
},
"team2": {
"key": "everton",
"name": "Everton",
"code": "EVE"
},
"score1": 2,
"score2": 0
},
...
]
}
]
}

Expected Result in JSON Format:


"clubs": [
{
"key": "chelsea",
"name": "Chelsea",
"code": "CHE",
"total_matches":38,
"won":"21",
"lost":"15",
"ties":"2",
"total-goals":"33",
"scored-against":"28"

},
{
"key": "arsenal",
"name": "Arsenal",
"code": "ARS",
"total_matches":38,
"won":"21",
"lost":"15",
"ties":"2",
"total-goals":"33",
"scored-against":"28"
}]

Answer №1

If you're looking for a solution, something along these lines could be the way to go. Just make sure to include additional error checking and make some modifications to achieve the desired outcome.

let dataA = {
    "name": "English Premier League 2011/12",
    "clubs": [{
            "key": "chelsea",
            "name": "Chelsea",
            "code": "CHE"
        },
        {
            "key": "arsenal",
            "name": "Arsenal",
            "code": "ARS"
        },
        {
            "key": "tottenham",
            "name": "Tottenham Hotspur",
            "code": "TOT"
        }
    ]
}
let dataB = {
    "name": "English Premier League 2011/12",
    "rounds": [{
        "name": "Round 1",
        "matches": [ ... // Data truncated for brevity
        ]
    }]
}

// Rest of the code remains unchanged...

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

Tips for retrieving the content of an input field using Angular

function HomeController($scope, navbarService) { var vm = this; $scope.showHeader = true; $scope.userNameNav =''; $scope.$on('toggleHeader', function(event, data) { $scope.showHeader = data; }); $s ...

Substitute placeholders in array with information using a loop

I have a question regarding implementing an autosort feature in JavaScript. I want my page to automatically sort data rows based on different time intervals selected by the user through checkboxes. The data updates every 3 seconds, and the autosort functio ...

Examining variable values and key names using periods in Twig

When working with my CakePHP view element to output a script tag, I utilize a variable named scriptsGroup which is assigned a value such as main. Using Twig: {{ dump(scriptsGroup) }} This results in: string(4) "main" Additionally, I have a view array ...

Data is not found in req.body when making a fetch request

I have been attempting to send a fetch request to my server, but I am consistently receiving an empty req.body. Here is the client-side script: const form = document.getElementById('form1') form.addEventListener('submit', (e) => ...

What is the method to create a resizable table column border rather than resizing the bottom corner border?

At the moment, we are able to resize the table border when we drag the corner. However, my goal is to enable resizing on the right side of the full border. https://i.sstatic.net/yFI24.png Below is the CSS code for resizing: th{ resize: horizontal; ove ...

Creating a regular expression variable in Mongoose: A step-by-step guide

I am looking for a solution to incorporate a variable pattern in mongoose: router.get('/search/:name', async(req, res) => { name = req.params.name; const products = await Product.find({ name: /.*name*/i }).limit(10); res.send(prod ...

Develop a fresh AngularJS directive that utilizes the HTML5 audio element

Recently delved into Angular and managed to create audio buttons using an Angular directive: app.directive('soundButton', [function () { return { restrict: 'E', link: function (scope, element, attrs) { v ...

the deactivation of my rollover class is being caused by my float class

With the assistance of a generous contributor on stackoverflow, I was able to overlay different colored CSS boxes onto various images. These boxes could be removed upon mouseover, revealing the images beneath. You can view the code I used on fiddle here. ...

Creating websites with Single-page applications by assembling HTML and JS fragments at build-time

As I prepare to tackle a large single-page app project, my main focus is on finding a more efficient way to develop it other than cramming everything into one massive file. My priority is ensuring the maintainability and testability of the app, which is wh ...

Dynamic class name changes in Angular.js based on JSON object

I am trying to dynamically change the class of an <li> element based on the category value I am getting, but for some reason the class name won't update. Here is the code snippet: <div id="content"> <ul id="container" ng-controller ...

Can you help me understand how to retrieve the position of a clicked object or sprite in three.js?

My JavaScript skills are lacking... T_T Is it possible to retrieve the position (x, y, z) where I clicked on a sprite or object? Here is the code I have so far. Can someone help me out? // Creating a sprite var spriteMap = ...

The combination of a modal box, checkbox, and cookie feature creates

I am trying to accomplish the following tasks: When the homepage loads, I want a modal box to appear Inside the modal box, there should be a form with a mandatory checkbox After checking the checkbox, submit the form and close the modal box to return to ...

Adding a checkbox to menu items in a ReactJS application can be accomplished by utilizing specific components

I have successfully created dynamic menus using a recursive function, and they are displaying in the correct order without any issues. Requirement: My current goal is to identify the last level of menus and assign a checkbox with the value corresponding ...

npm is not functioning properly in running React JS

I encountered an issue when trying to run my React project. This error message keeps popping up, and I'm not sure how to resolve it. Can anyone help me with this? W:\Burgerapp>npm start npm ERR! code ENOENT npm ERR! syscall open npm ERR! path ...

What methods can be used to identify the presence of a scrollbar in an

Currently, I am facing an issue with my SAPUI5 app. In the app, I have a table that extends beyond the screen. To allow users to navigate back to the top of the table, I have added a button. However, I only want this button to be displayed when the table g ...

Consistently obtaining the same outcome in JavaScript, always

Is it possible to resolve this issue? I keep getting a result of less than 18 when trying numbers 1-100, even though the output should be for values under 18. In my HTML code, there is a <p> element with id="result", an input with id=&quo ...

Exploring the presence of a specific value within a JSON array that is nested

I am working with a SQL 2016 table that has a column storing JSON data. Here is an example of the JSON structure: { "_id": "5a450f0383cac0d725cd6735", "firstname": "Nanette", "lastname": "Mccormick", "registered": "2016-07-10T01:50:10 +04: ...

Only one ID is recognized by jQuery

I have a group of three checkboxes set up like this: <input id="image_tagging" class="1" type="checkbox" value="1" checked="checked" name="data[image_tagging]"> Now, I've created an AJAX function (which is functioning correctly), but it seems ...

Obtain headers from receiving an external JavaScript file

I am trying to find a way to import an external .js file and access the response headers from that request. <script src="external/file.js?onload=callback"> function callback(data) { data.getAllResponseHeaders(); } </script> Unfortunately, ...

Angular Access Control

Troubleshooting the issue with $routeChangeStart, I noticed that even if the user is not found, it still allows access to pages when directly typing the URL. To address this, I had to update the server rules as follows: Options +FollowSymlinks RewriteEng ...