In my React.js project, I am looking to iterate through an array of objects and organize them based on a specific condition

Here is an example of the structure of my JSON file:

 {
    "PlanetIdentifier": "KOI-1843.03",
    "TypeFlag": 0,
    "PlanetaryMassJpt": 0.0014,
    "RadiusJpt": 0.054,
    "PeriodDays": 0.1768913,
    "SemiMajorAxisAU": 0.0048,
    "Eccentricity": "",
    "PeriastronDeg": "",
    "LongitudeDeg": "",
    "DiscoveryYear": 2012
  },
  {
    "PlanetIdentifier": "KOI-1843.01",
    "TypeFlag": 0,
    "PlanetaryMassJpt": "",
    "RadiusJpt": 0.114,
    "PeriodDays": 4.194525,
    "SemiMajorAxisAU": 0.039,
    "Eccentricity": "",
    "PeriastronDeg": "",
    "LongitudeDeg": "",
    "DiscoveryYear": ""
  }

My current task is to arrange the objects by year and then by RadiusJpt.

if(RadiusJpt > 2) then it is classified as a large planet
if(RadiusJpt < 1) then it is classified as a small planet
else it is classified as a medium-sized planet

After sorting, I need to display the count of small, medium, and large planets discovered each year.

Answer №1

Employ the Array#sort method:

const planets = [{
    "PlanetIdentifier": "KOI-1843.03",
    "TypeFlag": 0,
    "PlanetaryMassJpt": 0.0014,
    "RadiusJpt": 0.054,
    "PeriodDays": 0.1768913,
    "SemiMajorAxisAU": 0.0048,
    "Eccentricity": "",
    "PeriastronDeg": "",
    "LongitudeDeg": "",
    "DiscoveryYear": 2012,
  },
  {
    "PlanetIdentifier": "KOI-1843.01",
    "TypeFlag": 0,
    "PlanetaryMassJpt": "",
    "RadiusJpt": 0.114,
    "PeriodDays": 4.194525,
    "SemiMajorAxisAU": 0.039,
    "Eccentricity": "",
    "PeriastronDeg": "",
    "LongitudeDeg": "",
    "DiscoveryYear": 2010,
  },
    {
    "PlanetIdentifier": "KOI-1843.01",
    "TypeFlag": 0,
    "PlanetaryMassJpt": "",
    "RadiusJpt": 0.5,
    "PeriodDays": 4.194525,
    "SemiMajorAxisAU": 0.039,
    "Eccentricity": "",
    "PeriastronDeg": "",
    "LongitudeDeg": "",
    "DiscoveryYear": 2010,
  }
]

const sortedPlanets = planets.sort((planet1, planet2) => {
  let difference = planet1.DiscoveryYear - planet2.DiscoveryYear;
  return difference == 0 ? planet1.RadiusJpt - planet2.RadiusJpt : difference;
})

console.log(sortedPlanets);

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

Wildcard GET requests in Express cause routing issues when navigating to a React application

My project is derived from the React-Webpack boilerplate repository found at this link: (https://github.com/wallacyyy/webpack-heroku/blob/master/server.js). Initially, everything was working fine but now I want to add routing within my React application. T ...

Efficiently transferring time values from dynamically created list items to an input box using JavaScript

Is there a way to store a dynamically generated time value from an li element into an input box using JavaScript? I have a basic timer functionality on my website that includes starting, stopping, pausing, taking time snaps, and resetting them. These time ...

What could be causing the datepicker to not acknowledge any selected options?

I'm currently facing an issue where I am trying to populate a select dropdown with available hours that do not have appointments booked. The problem seems to be related to the datepicker plugin behavior. I want the dropdown to be populated based on th ...

Exploring the implementation of method decorators that instantiate objects within Typescript

Currently, I have been working on a lambda project and utilizing the lambda-api package for development. As part of this process, I have implemented decorators named Get and Post to facilitate mapping routes within the lambda api object. These decorators e ...

PHP - Extracting an array of objects from a database query output

Here's a simplified version of the issue: I have a query inside a function that retrieves data from users table. $query = "SELECT * FROM users"; $result = mysql_query($query); I want to use mysql_fetch_object() to get an object from each row in the ...

Freezing in IE/Safari due to AJAX jQuery interactions

Feeling frustrated and seeking help. I am currently executing this particular script: <script type="text/javascript" charset="utf-8> jQuery(window).ready(function(){ //alert('running'); var sold = false; var l ...

What is the best way to turn each function within the module pattern into a promise?

Utilizing Angular 1.5, I have developed a factory function that returns a literal object structured as follows: return { item: null, get: function() { return item; }, create: function() { if (this.get()){ this.remove(); ...

Identifying child elements in jQuery with identical IDs

Consider this HTML setup: <div id="contentRead"> ... <div id="List"></div> </div> ... <div id="contentWrite"> ... <div id="List"></div> </div> ...

Delay in Response of OnTextChanged Event in ASP.NET

The functionality is there, but it doesn't respond immediately. It only works when I click on something. I attempted using the onkeyPress method as well: <asp:TextBox ID="txtWriter" runat="server" onkeyPress="txtOnKeyPress" ></asp:TextBox& ...

Grab the source attribute of the <img> tag (identified by class) across numerous HTML pages

Here is an example: A website has the URL , where xxxx represents an integer between 1 and 1935. On each page, there may be an <img class="thumbnail" src="Images\Robots\{robotname}.png">. However, not all pages have th ...

Tips for transferring a jQuery array to PHP

I am encountering an issue when trying to send a jQuery array to PHP. Initially, I have one form in HTML and upon clicking 'add', I end up with two forms. Afterwards, I input data into the form which is then stored in a jQuery array. However, I a ...

How can I create a new div element for every item in an array by utilizing JSX?

I am currently working on creating a div element for each item in an array using reactjs JSX. I wasn't sure how to tackle this, so I attempted the following approach: {results.map((element, index) => ( <div key={index} className={ ...

What is the syntax for entering an array in C++?

What is the process of entering an array in C++? For example, inputting - 2 3 56 and storing it in array A[0]=2, A[1]=3, A[2]=56. ...

Enhance Your Website's User Experience with Jquery Link Click

In the layerslider, I have a form where users must fill out the current page to move to the next one. Initially, I tried showing and hiding the button, but it confused my users. Now, I am changing the opacity and clickability of the buttons based on page ...

Angular's '@angular/core/core' module does not have the exported member 'ɵɵFactoryDeclaration'

My Angular application was successfully compiled after running npm install. However, upon executing npm start, I encountered the following error: ERROR in node_modules/ng-apexcharts/lib/chart/chart.component.d.ts:57:21 - error TS2694: Namespace '&quo ...

Encountering an issue while attempting to make changes and test a copied GitHub library - npm ERROR! version cannot be located

I'm new to the world of Github forking and pull requests. My goal is to fork a repository, make some changes, and test them out on a project before submitting a pull request. I've already forked the repository and made modifications, but I' ...

Tips on removing properties from an object recursively according to their key/value pairs

My current task involves removing specific children of an object based on whether their "size" key is set to 0. To achieve this, I am utilizing the npm package directory-tree to generate a JavaScript object representation of a chosen directory. The stru ...

Providing clients with JSON-formatted messages and data through Java/Spring REST services

In order to customize the response from my endpoint, I want to include a special message along with the data in json format. For example: { "id": "1", "name": "John", "surname": "Jackson", &qu ...

Resource for building user interface components in Javascript

I need assistance with implementing a feature that involves scrolling through different text blocks and corresponding images. Users should be able to select a specific text block and have the associated image on the right scroll into view, similar to a car ...

Tips for utilizing the select feature within an ng-repeat loop while maintaining the selected value when fetching data from an API

I am currently facing an issue with using select inside ng-repeat. I am attempting to correctly map the value coming from my API to the select as the selected value. However, I seem to be missing something from my end. Can someone please help me identify a ...