Discover the minimum and maximum values within a JavaScript array in order to assess the array's properties

I am working on extracting the minimum and maximum values from a multidimensional JavaScript array based on two specific fields.

The data I receive is in timeseries format from an IoT device:

timestamp, device id, value1, value2, value3, etc.

To achieve this, I need to create a function that can determine the min/max values for each device based on a particular field.

Below is a sample of the array:

[
    [
        1577912341,
        "ITEM1",
        131.98840369
    ],
    [
        1577998742,
        "ITEM2",
        127.27418424
    ],
    [
        1578085141,
        "ITEM2",
        133.67909395
    ],
    [
        1578171541,
        "ITEM1",
        134.82513897
    ]
]

I am looking to find the min/max value for each unique item in field 1 using the value in field 3. For instance, I want to identify the max value of field 3 for (ITEM1), which in this case would be "134.82513897".

The min value of field 2 (ITEM1) is 131.98840369. The max value of field 2 (ITEM1) is 134.82513897.

The min value of field 2 (ITEM2) is 127.27418424. The max value of field 2 (ITEM2) is 133.67909395.

Answer №1

Here is a code snippet that could help you kickstart your project.

const data = [
    [
        1577912341,
        "ITEM1",
        131.98840369
    ],
    [
        1577998742,
        "ITEM2",
        127.27418424
    ],
    [
        1578085141,
        "ITEM2",
        133.67909395
    ],
    [
        1578171541,
        "ITEM1",
        134.82513897
    ]
];
const items = [...new Set(data.map(d => d[1]))];
console.log(items);

const results = items.map(item => {
  const subset = data.filter(d => d[1] === item).map(d => d[2]);
  return {
    item: item,
    min: Math.min(...subset),
    max: Math.max(...subset)
  };
});
console.log(results);

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

In MongoDB and Node.js, encountered error: 'Unable to access property 'collection' as it is undefined'

I'm encountering a type error with my collection function that was previously functioning without any issues. This code was working perfectly in another project of mine. After reusing a lot of the code, I started experiencing this error. My MongoDB c ...

Automated deletion of text input in v-textarea after a specified duration

When using a v-textarea, is it possible to automatically clear the content after a certain amount of time (2 minutes) if no action is taken? If so, how can this be achieved? https://i.sstatic.net/16EAj.png <v-textarea v-model.trim="text& ...

Encountering a Zone.js error when trying to load an Angular 7 app using ng serve, preventing the application from loading

Scenario: Yesterday, I decided to upgrade my Angular app from version 5.2.9 to 6 and thought it would be a good idea to go all the way to 7 while I was at it. It ended up taking the whole day and required numerous changes to multiple files, mostly due to R ...

Modifying the anchor link in a pop-up window according to the clicked link using JQuery

I'm currently working on enhancing a forum where we aim to alert users when they click on links that will redirect them to an external website. Right now, my code successfully opens a pop-up window and directs the user to the intended link. However, ...

Modify the ngb-timepicker formatting to output a string instead of an object

I am currently working on modifying ngb-timepicker so that it returns a string instead of an object. Right now, it is returning the following format: { "hour": 13, "minute": 30 } However, I want it to return in this format: "13:30" This is the HTM ...

What methods can I utilize to showcase the JSON data on my webpage efficiently?

I'm currently working on a script that makes an ajax request. The Cloud appears to be responding with JSON data, but I'm not sure how to display this data on my webpage. Any suggestions? Here you can find a link to view the neatly formatted JSON ...

I encountered an issue when trying to include the dotenv file, receiving the following error message: [TypeError: Network request failed]

babel.config.js File plugins: [ ["module:react-native-dotenv", { "envName": "APP_ENV", "moduleName": "@env", "path": ".env", "blocklist": null, "allowlist": null, "blacklist": null, // DEPRECATED "whitelist": ...

Animate the movement of a div

Within the given code snippet, the .logo element is initially hidden. The goal is to make it visible upon scrolling, while simultaneously animating the movement of the <ul> element to the right (e.g., sliding). Upon reviewing the demo provided, one c ...

Customize chart.js for your specific region

(I prefer not to utilize node and npm) To have a local copy of chart.js on my machine for offline use, I obtained the cdn file from this source. I then imported it into my html code like so: <script type="importmap"> { &q ...

Package.json file is not included in Typescript

Each time I execute tsc, it converts the files to JS format successfully, except for package.json. I want this file included in my output directory. Currently, my tsconfig.json looks like this: { "exclude": ["node_modules"], "compilerOptions": { " ...

CrossBrowser - Obtain CSS color information

I'm attempting to retrieve the background color of an element: var bgcolor = $('.myclass').first().css('background-color') and then convert it to hex function rgbhex(color) { return "#" + $.map(color.match(/\b(\d+ ...

Use Javascript to display an image based on the date, otherwise hide the div

I'm looking to implement an image change on specific dates (not days of the week, but actual calendar dates like August 18th, August 25th, September 3rd, etc). Here's the div I'm working with: <div id="matchday"> <img id="home ...

Storing three values in a Java array

I am in need of an array object that can hold 3 objects. Initially, I tried using Collection, but I require the ability to add at a specific position. For example: CollectionObject.add(pos, myObject) So then I turned to ArrayList and created the object ...

AngularJS - Issue with Checklist-Model not accurately reflecting changes in the model when checkboxes are toggled

Recently delving into AngularJS, I encountered a puzzling issue with the Checklist-Model directive. To replicate the problem, I followed one of their examples. Upon clicking a checkbox and invoking a function, the model reflects the update correctly on th ...

Error: The array contains an undefined image object

Currently, I am implementing a simple image preloading technique using promises. This function is integrated into a larger Angular (1.3.15) custom directive. Here is the JavaScript code snippet: function preLoad() { var deferred = $q.defer(); ...

Tips on saving php variable content in HTML "id"

Three variables are used in PHP: message_id, message_title, and message_content. Their content is stored inside HTML 'id' for later use with jQuery. Example: Variables: $id_variable = $rows['id_mensagem']; $message_title_edit = $rows ...

Failing to transfer a string from fixed memory to allocated memory

I created a function that prompts the user for a string input. It then dynamically allocates memory to store the input data. However, after 3 inputs, the program unexpectedly crashes. void userInputStrings(int totalInputs, char **inputArray) { char ...

Issue with narrowing TypeScript arrays often encountered

When working with arrays of strings in my TypeScript code, I restrict the contents to certain letters by using a defined type like ("A" | "B")[] for letters such as A and B. However, when I have a function that takes an arbitrary array ...

Transferring JavaScript objects from child process to parent through streaming

Looking for a simple method to stream nodejs objects from a child process to the parent. I am currently working on separating a lengthy pipeline that processes objects through different components. Now, I plan to divide the chain and move part of it into ...

TS1057: It is required that an async function or method has a return type that can be awaited

There was a recent Github issue reported on March 28th regarding async arrow functions generating faulty code when targeting ES5, resulting in the error message: TS1057: An async function or method must have a valid awaitable return type You can find t ...