Determine the maximum value in an array that includes both numerical and non-numeric values

My goal is to determine the maximum value for 15-minute data that I am extracting from a .csv file. Each row in the file represents 15-minutes of data from a different source identified by "ID" on a specific day indicated by "Date". However, there are unnecessary columns such as "Class", "Sample", and "Channel" that are not relevant to my analysis.

I initially considered using d3.group, but having five key values corresponding to the non-numeric attributes does not seem to be the most effective approach.

The format of the imported data is as follows:

[
  {
    "ID": "10D1012B",
    "Class": "R",
    "Sample": "135_OLD",
    "Channel": "1",
    "Date": "1/1/2019",
    "00:15": "0.48",
    "00:30": "0.6",
    "00:45": "0.896",
    "01:00": "0.712",
    "01:15": "1.12",
          ...
    "23:00": "0.48",
    "23:15": "0.6",
    "23:30": "0.624",
    "23:45": "0.552",
    "00:00": "0.704"
  },
  {
    "ID": "10D1040B",
    "Class": "R",
    "Sample": "135_OLD",
    "Channel": "1",
    "Date": "1/2/2019",
    "00:15": "1.016",
         ...
    "23:30": "0.632",
    "23:45": "0.72",
    "00:00": "0.776"
  }
]

To establish the chart scale, I need to compute the highest value within each row of data (identified by ID and Date). However, I am unsure about the appropriate method to exclude the non-numeric columns when determining the maximum value.

Answer №1

Two issues need to be addressed in your code: firstly, the d3.max function is traversing through different objects within the same array, when you actually want it to traverse through different values within the same object. Secondly, there are numerical values, like those for the "Channel" property, that should not be included. This makes the task more complex than simply identifying all numerical values in the properties.

To tackle this, I suggest creating a list of property names or keys that should be excluded from consideration:

const nonNeededKeys = ["ID", "Class", "Sample", "Channel", "Date"];

Then, utilize d3.max for all other keys within each object in the array:

data.forEach(function(row) {
  const keys = d3.keys(row).filter(function(d) {
    return nonNeededKeys.indexOf(d) === -1;
  });
  row.maxValue = d3.max(keys.map(function(d) {
    return +row[d]
  }))
});

In this approach, a new property named maxValue is created for each object.

A demo showcasing this method can be found below:

const data = [/* Data array goes here */];
// Rest of the script as shown
console.log(data)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>

Subsequently, determining the maximum value across all objects becomes straightforward:

const maxValue = d3.max(data, function(d) {
    return d.maxValue;
});

Check out the snippet below to see the process in action:

const data = [/* Data array goes here */];
// Rest of the script as shown
const maxValue = d3.max(data, function(d) {
  return d.maxValue;
})
console.log(maxValue)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>

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 unprojection of the vector in Threejs from this camera has not been defined

Why am I encountering a "function is undefined" error for vector.unproject even though it is clearly mentioned in the documentation? You can find it here: (one of the last items) Even after console logging it, the function still returns as undefined, des ...

Unleashing the Power of Aurelia's HTML Attribute Binding

I need to bind the "required" html attribute in my template and model. Along with passing other information like the label value using label.bind="fieldLabel", I also want to pass whether the element is required or not. However, simply using required="tr ...

Is it possible to transfer data from javascript to php through ajax?

I am attempting to extract the data speedMbps from my JavaScript code using Ajax to send the data to my PHP script, but unfortunately, I am not receiving any output. My experience with Ajax is limited to implementing auto-completion feature. <script sr ...

angular application developed using HTML, CSS, and JavaScript

With my current Angular project, I am working with 4 components (comp1, comp2, comp3, comp4). I came across a code snippet for a form consisting of HTML, CSS, and JavaScript that I want to integrate into comp1. Can you please confirm if I should place the ...

Can you please explain the significance of the code "!!~this.roles.indexOf('*')" within the MEAN.io framework?

One particular line of code can be found in the startup file for the MEAN framework. if (!!~this.roles.indexOf('*')) { This specific line is located within the shouldRender function of the menus.client.service.js file, which resides in the publ ...

Is there a way for me to obtain the full error message after a failed fetch request?

I'm trying to capture all errors from the fetch function, including the specific red highlighted details as a string: https://i.sstatic.net/GtHxv.png But when I catch an error in my code, all I get is "Failed to fetch." Here's what my code looks ...

Limiting the space character in a password field with regular expressions in JavaScript

Currently, I am facing the challenge of implementing a RegEx expression on a property within a model on an MVC website that I am currently developing. While I understand the individual components of the expression, I am struggling to combine them effectiv ...

I am attempting to retrieve JSON data in JavaScript but am seeing a null response in the action class

Having an issue with JSON data being sent through AJAX from the client side. The data is showing as null inside my action class. Here is the JSON string: {"data":[{"id":"","col":1,"row":1,"size_x":1,"size_y":1}, {"id":"","col":1,"row":2,"size_x" ...

I am looking to modify the brand name that is being displayed in a while loop using AJAX. The information is stored in an input field of type "text

I need to modify the brand name within a while loop using AJAX. The data is in an input type="text" field, but I am encountering an issue with sending the data via the id attribute because the id needs to be unique. Is there a way to send the data without ...

Execute the setTimeout function when scrolling occurs

I need help delaying a timeout effect. Currently, the effect begins as soon as the page loads, but I want it to start when I scroll to a specific div. You can see how it currently functions here. After some research, I came across a library called waypoin ...

Achieving the sorting of empty data to be at the end using jQuery

var data = [{ "id": "2" }, { "id": "3" }, { "id": "4" }, { "id": "5" }, { "id": "6" }, { "id": "7" }, { "id": "1" }, { "id": "" }, { "id": "" }, { "id": "" }] $('.up').click(function() { var prop = $(this).closest('th& ...

"Exploring Mocha and Chai: Tips for Generating a Comprehensive List of Errors in Test Cases

I am currently working on a unit test to validate a list of elements. Example: arr = [ { element : "aaa", validation : false }, { element: "bbbb", validation: true }, { element: "ccc", validation: false } While running my unit test, I encountered ...

Navigating through an empty array while implementing a combination of map and filter in react.js

I have an array containing NFT details, where I filter the NFTs by contract address and then map them to their individual components. Everything is functioning correctly, however, the issue arises when there are no matching NFTs. In this case, the filter ...

Accessing this.href from the ASP.NET code behind using Javascript

I'm trying to pass this.href from my asp.net code behind to JavaScript. When I run the code, the value appears as 'undefined' in the alert message. Can anyone help me with this issue? Page.ClientScript.RegisterStartupScript(this.GetType(), ...

Unlocking the power of executing Node.js functions with JavaScript

As I work on developing a Java web application, I need to access the environment variables of the user's machine when they open my site. Specifically, I require the username and domain information in order to leverage single sign-on technology. I&apos ...

Tips for displaying four distinct pages with d3js graphs in four different divs on a single webpage using jQuery or JavaScript

I've tried numerous methods, but I'm struggling to get it to work correctly. What assets do I have? 1 index page with a table and 4 hidden div elements; 4 HTML pages with various content (graphs). What am I aiming for? To have each div disp ...

What is the best way to shorten text in React/CSS or Material UI based on the line height multiples?

I'm facing an issue with creating a Material UI card that contains text. My goal is to set a fixed height for the card and truncate the text if it exceeds 3 lines. Can anyone suggest the best approach to achieve this? Here's the code snippet I&a ...

Using jQuery to implement tiered radio buttons styled with Bootstrap

I am currently in the process of learning jQuery, so please excuse my lack of knowledge in this area. I am working on a web form using Bootstrap that involves tiered radio buttons. When the user selects "Yes" for the first option, it should display the ne ...

Tips for managing player rounds updates

How can I make the number of rounds increase by 1 every time a card is clicked, rather than jumping to 4 and staying there? The initial value is set at 'let rounds = 0;' in my code. <!DOCTYPE html> <html lang="en"> < ...

Each loop in the forEach function triggers the mouseup event

I am currently utilizing a foreach loop: objects.forEach(function(object) { var button = '<tr><td>' + object.object.code + '</td><td>' + formatDistance(1456000) + &apos ...