Tips for verifying if an array contains elements that exceed a certain value

In this scenario, I have an array that needs to be checked for any AvgWT value.

  1. If the value is greater than 15000 and less than 20000, then return 50.
  2. If the value is greater than 10000 and less than 15000, then return 100.
  3. If the value is greater than 5000 and less than 10000, then return 150.

I attempted the following code.

var data = [{"TimeSlot":"8:00 am","AssistedC":0,"Abandons":0,"AvgWT":12152},{"TimeSlot":"8:15 am","AssistedC":0,"Abandons":0,"AvgWT":13942},{"TimeSlot":"8:30 am","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"8:45 am","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"9:00 am","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"9:15 am","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"6:30 pm","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"6:45 pm","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"7:00 pm","AssistedC":0,"Abandons":0,"AvgWT":0}];

function checkArrayValues(data) {
   // var first = array[0];
    var first = data[0]["AvgWaitTime"];
    return data.every(function(element) {
        if(element["AvgWaitTime"] > 15000 && element["AvgWaitTime"] < 20000){
            return 50;
        }else if(element["AvgWaitTime"] > 10000 && element["AvgWaitTime"] < 15000){
            return 100;
        }
    });
}

isAllValuesSame = checkArrayValues(data);

Unfortunately, the code is not functioning correctly. Can you help identify the mistake I made?

Answer №1

If I understood correctly, you can utilize the .map() method to create a new array containing elements that meet specific conditions. Here is an example of how you can achieve this:

const arry = [{"TimeSlot":"8:00 am","AssistedC":0,"Abandons":0,"AvgWT":12152},{"TimeSlot":"8:15 am","AssistedC":0,"Abandons":0,"AvgWT":13942},{"TimeSlot":"8:30 am","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"8:45 am","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"9:00 am","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"9:15 am","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"6:30 pm","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"6:45 pm","AssistedC":0,"Abandons":0,"AvgWT":0},{"TimeSlot":"7:00 pm","AssistedC":0,"Abandons":0,"AvgWT":0}];

function checkIfMatchCondition({ AvgWT: value }) {
  if(value < 20000 && value > 15000)
    return 50;
  else if(value < 15000 && value > 10000)
    return 100;
  else if(value < 10000 && value > 5000)
    return 150;
  return 0;
}

const res = arry.map(checkIfMatchCondition);
console.log(res)

There is potential for improvement by including the index or additional properties in the original object, but more information is needed for further enhancements.

Answer №2

If you're looking to check for specific conditions in an array of values, you can achieve this using a simple for loop.

Feel free to test the snippet below to verify the output, as I've made some assumptions based on the information provided:

  1. Were you expecting a single value or an array as the output? The code below generates an array with values corresponding to the conditions specified.

  2. What should happen when the value in AvgWT falls outside the given ranges? The code assigns a value of 0 in such cases.

If there are any misunderstandings or if you would like adjustments, please let me know!

function checkValueInArray(array) {

  var outputArray = [];

  for (var i = 0; i < array.length; i++) {
    var element = array[i]["AvgWT"];

    if (15000 < element && element < 20000) {
      outputArray.push(50);
    } else if (10000 < element && element < 15000) {
      outputArray.push(100);
    } else if (5000 < element && element < 10000) {
      outputArray.push(150);
    } else {
      outputArray.push(0);
    }
  }

  return outputArray;
}



var sampleArray = [{
  "TimeSlot": "8:00 am",
  "AssistedC": 0,
  "Abandons": 0,
  "AvgWT": 12152
}, {
  "TimeSlot": "8:15 am",
  "AssistedC": 0,
  "Abandons": 0,
  "AvgWT": 13942
}, {
  "TimeSlot": "8:30 am",
  "AssistedC": 0,
  "Abandons": 0,
  "AvgWT": 0
}, {
  "TimeSlot": "8:45 am",
  "AssistedC": 0,
  "Abandons": 0,
  "AvgWT": 0
}, {
  "TimeSlot": "9:00 am",
  "AssistedC": 0,
  "Abandons": 0,
  "AvgWT": 0
}, {
  "TimeSlot": "9:15 am",
  "AssistedC": 0,
  "Abandons": 0,
  "AvgWT": 0
}, {
  "TimeSlot": "6:30 pm",
  "AssistedC": 0,
  "Abandons": 0,
  "AvgWT": 0
}, {
  "TimeSlot": "6:45 pm",
  "AssistedC": 0,
  "Abandons": 0,
  "AvgWT": 0
}, {
  "TimeSlot": "7:00 pm",
  "AssistedC": 0,
  "Abandons": 0,
  "AvgWT": 0
}];

console.log(checkValueInArray(sampleArray))

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 Node.js error message "Module not found"

Currently, I am working through the node.js tutorial on Lynda.com and encountering an issue with the "Error: Cannot find module". Despite having the flight module listed in the package.json file, the error persists. Up until this point, everything has bee ...

Is there an XML File Wrapper to Generate PDF Output?

Greetings Forum Members, I have been given the responsibility of creating a PDF file from multiple XML files. Has anyone come across an XML wrapper file before? This type of file would essentially contain a list of all the source XML file names in a spec ...

A custom script developed to detect the presence of the numeric combination "11" and promptly notify the

I am attempting to develop a unique chrome extension that detects typing errors in orders. For example, if the user accidentally types "11" instead of "1", an alert should be triggered. However, I am encountering an issue where the script is running in an ...

Determine the coordinates of the mouse cursor within a specific div element

Similar Questions: Determining Mouse Position Relative to a Div Getting the Mouse Position Using JavaScript in Canvas Is there a way to retrieve the mouse position within a canvas element with fixed dimensions and automatic margin? I am unable to ...

Extracting public data from social media profiles as displayed in Smartr

Is there any pre-existing API or reference material available for achieving this task? I am interested in accessing public social data without the need for users to manually link their accounts to our site. ...

Is it possible to use JavaScript to retrieve a client's bookmarks with their consent?

Is there a way to streamline the process of importing bookmarks to my server for users? Can I use JavaScript to automatically retrieve a user's bookmarks, or is that not possible due to security concerns in browsers? ...

Using a data() object in Vue to fill out form fields efficiently

My data retrieval process from mongodb involves obtaining the data and transferring it to the client side using the following approach: error_reporting(E_ALL); ini_set('display_errors', '1'); require '../vendor/autoload.php'; ...

How can I enter a single backslash for location input in node.js without using double backslashes?

I have a project where the user needs to input a word to search for in files along with the folder location. However, I am struggling to write code that allows the user to input the location using single backslashes instead of double backslashes. const fol ...

Tips on restricting users to choose dates that are later than the current date

Currently, I am working with Vue3 using the options API. After reviewing this StackBlitz, my question is regarding how to correctly set the :max value for a date-picker. Even though I have assigned :max as new Date(), I am still able to select dates that ...

Is handlebars.js giving you trouble all of a sudden?

My handlebars.js template was working perfectly for a week, but suddenly stopped. I'm puzzled by this issue and would appreciate any insights on why it might have stopped functioning. Here is the template: <script id="banners-template" type=" ...

Setting the attribute dynamically for a select box with multiple choices

In my form, I have multiple choice select boxes styled using bootstrap select. Since the app is developed in Express, I am having trouble retrieving the selected values due to bootstrap select creating a div and a list for user interaction. To tackle this ...

Problem with Layering in Javascript Canvas Game using Kinetic JS

Currently, I am attempting to replicate the game found at and delving into html5 canvas and kineticJS for my study. Here is the link to my fiddle: http://jsfiddle.net/2WRwY/7/ The issues I am facing are as follows: The tail part of the player in the f ...

Using Javascript and jQuery to create an infinite animated background change with fade effect

I am struggling to figure out how to automatically change the background of a div every 3 seconds, looping through a series of images. I posted about this issue before but didn't receive much help. function animate() { change1(); change2() ...

Dynamic menu bar accompanied by primary content

I'm facing an issue with my active navigation bar. Whenever I open the hamburger menu, the main content remains fixed and does not move along with the open navigation menu. I tried searching online for a solution but couldn't find anything helpfu ...

Leveraging custom properties in HTML elements with JavaScript

I am in the process of creating a straightforward battleships game that utilizes a 10x10 table as the playing grid. My goal is to make it easy to adjust the boat length and number of boats, which is why I'm attempting to store data within the HTML obj ...

The useEffect alert is triggered before the component is re-rendered

Can someone help me understand why the HP in the code below is displayed as "1" when the alert is thrown, and only set to "0" after I confirm the alert? Shouldn't the component be rerendered before the alert is thrown so that it has already been displ ...

Issue with dynamic form JavaScript functionality after removing curly braces { } from a select tag in Rails

In my Rails form, there is a gender field defined as follows: <%= f.select :gender, ["Male","Female"],{class: "gender"} %> I also tried adding an onclick event like this: <%= f.select :gender, ["Male","Female"],{class: "gender"},onclick: "categ ...

Obtain the child's identification number and include it in the parent element as an ARIA

I need assistance with a JavaScript (or jQuery) function to dynamically add an 'aria-labelledby' attribute to parent <figure> elements based on the ID of the <figcaption>. I have multiple images and captions set up in <figure>&l ...

Issues with Converting JSON to HTML Table using JavaScript

Issues with Converting JSON to HTML Table Using JavaScript I've been trying to create a function that will display the contents of a JSON file in an HTML table. However, I keep getting an undefined error. Despite being able to see the data displayed ...

Javascript for Cordova utilizing WebSocket and incorporating client side certificates

I am looking to establish a secure SSL/TLS connection between my client and server, with only a specific authorized client allowed to connect. To achieve this, I have generated a client certificate using OpenSSL on the server for mutual authorization. The ...