Ways to transform a group of strings into an array of separate strings

function categorizeMember(data) {
  let resultArr = []
  let userData = [data]
  return userData.forEach(data => {
    data.map(data => {
      let category = (data[0] >= 55 && data[1] > 7) ? console.log("Senior") : console.log("Open");
      return category
    })
  })
}

the function above should categorize members as senior or open in this form

[ 'Open', 'Senior', 'Open', 'Senior' ]
however, the output I received was: Open Senior Open Senior

an example of the expected output:

input = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]] 
output = ["Open", "Open", "Senior", "Open", "Open", "Senior"]

Answer №1

To determine if a person is a 'Senior' or 'Open', you can simply map the desired strings based on the values of the left and right items within the nested arrays.

function checkMembershipStatus(data) {
    return data.map(([left, right]) => left >= 55 && right > 7 ? 'Senior' : 'Open');
}

const data = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]];

console.log(checkMembershipStatus(data)); // ["Open", "Open", "Senior", "Open", "Open", "Senior"]

Answer №2

If you want to implement the code-template mentioned in the description, here is one way to solve it:

function checkMembershipStatus(data) {
  let statusArray = [];
  let userData = [data];
  userData.forEach(data => {
    data.map(memberData => {
      let status = (memberData[0] >= 55 && memberData[1] > 7) ? "Senior" : "Open";
      statusArray.push(status);
    });
  });
  return statusArray;
}

const data = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]];
console.log(checkMembershipStatus(data)); // ["Open", "Open", "Senior", "Open", "Open", "Senior"]

However, the above solution involves redundant operations. A more efficient approach would be:

function checkMembershipStatus(data) {  
    return data.map(memberData => (memberData[0] >= 55 && memberData[1] > 7) ? "Senior" : "Open");     
}

const data = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]];
console.log(checkMembershipStatus(data)); // ["Open", "Open", "Senior", "Open", "Open", "Senior"]

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

I am facing an issue with Angular reactive forms where the default values I set in ngOnInIt are not being reflected

Having some issues with setting default values in my Angular app using reactive forms. The defaults I set in ngOnInit are not showing up. I am also using the filter function within the map method. I am trying to select a value based on the URL and have it ...

Invoke a function from a different source in JavaScript

Below is the JS function implemented: function addMemberToLessonDirect(id) { $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } ...

Accept only hexadecimal color codes as user input

How can I create a variable in JavaScript that only accepts color codes such as rgba, hashcode, and rgb? I need a solution specifically in javascript. ...

The requested API endpoint for retrieving the name could not be found on the Express

Struggling to configure the restful API for my express app. Below is my app.js code: var express = require('express'), app = express(), bodyParser = require('body-parser'), methodOverride = require('method-override'); rout ...

Tips for incorporating tabs using jQuery

Check out this code snippet: <script> $(function() { $("#tabs").tabs({ event: "mouseover" }); $("#tabs").tabs("add","#tab-4","Friends Discussions"); $("#tabs").tabs("add","#tab-5","Announcements"); $("#tab ...

Converting a Powershell array into an HTML format

I have three arrays that I need to display on an HTML page: foreach($item in $array1){ // write array data to HTML } foreach($element in $array2){ // write array data to HTML } foreach($value in $array3){ // write array data to HTML } What is the ...

Verifying the activation status of a button within a Chrome extension

I have been working on a chrome plugin that continuously checks the status of a button to see if it is enabled. If it is, the plugin clicks on the button. I initially used an infinite for loop for this task, but realized that it was causing the browser to ...

Creating an API with an empty body using Express and Node.js

Currently, I am working on creating an API using Express in Node.js. The primary objective of this API is to receive a request containing a photo and then upload that photo to Firebase storage. However, I am facing a challenge where the body of the reque ...

Retrieve the Typescript data type as a variable

I have the following components: type TestComponentProps = { title: string; } const TestComponent: React.FC<TestComponentProps> = ({ title, }) => { return <div>TestComponent: {title}</div>; }; type TestComponent2Props = { bod ...

Node.js can provide a reusable email framework that can be used across

When it comes to sending emails based on specific business functions in nodejs, I have been following a particular approach. However, I am exploring the possibility of improving this method by composing email content more efficiently. Instead of hardcoding ...

Click on the first jQuery element

I'm currently dealing with a menu that has multiple levels. My goal is to create a functionality where clicking on a first-level element will add the class .ubermenu-active to it, while also removing this class from any other first-level elements. $( ...

Oops! The API request was denied with error code 401 - Unauthorized in React

I have been working on incorporating an API into my front-end project using React/Typescript. The documentation for the API specifies that authorization requires a key named token with a corresponding value, which should be included in the header. To stor ...

Incorrect footer navigation on my Vuepress website

I'm in the process of developing a vuepress single page application for showcasing and providing downloads of my personal game projects. When it comes to website navigation, I am aiming to utilize the native sidebar exclusively. This sidebar will hav ...

Can anyone explain why the Splice function is removing the element at index 1 instead of index 0 as I specified?

selectedItems= [5,47] if(this.selectedItems.length > 1) { this.selectedItems= this.selectedItems.splice(0,1); } I am attempting to remove the element at index 0 which is 5 but unexpectedly it deletes the element at index ...

Preserve the chosen option in a dropdown menu even after a postback using JavaScript

Seeking Help in Retaining Dropdownlist Selected Value After Postback In my efforts to retain a dropdownlist selected value after postback, I have been exploring various methods. I extract the selected values from the dropdownlist and store them in local ...

To retrieve the first td element by clicking a button within the corresponding row using JQuery

This may seem like a straightforward inquiry, but despite my best efforts, I have been unable to find a solution. I am looking to create a function that will allow me to click a button located in the 3rd td element and display the text from the first td e ...

If the href attribute is set to "#" or the target attribute is set to "_blank", then the <a> request cannot be prevented

I have a registration site that triggers a warning window whenever a user clicks on any link during the registration process. However, I want to exclude <a> tags with href="#" and target="_blank" attributes from this behavior. Essentially, I want to ...

Creating a hierarchical JSON format for a nested commenting system

I have a JSON data representing a multi-level comment system as shown below: [{ "thread_id": 2710, "parent_id": "", "username": "string", "comment": "string", "postdate": "2017-06-09T07:12:32.000Z", "id": 1 }, { "thread_id": 2710, "parent_ ...

Tips on stopping Bootstrap Modal from opening when element within trigger is clicked?

I've configured my table rows to trigger a bootstrap modal upon click. However, I have an issue with the select box in each row allowing user selection of status options. Is there a way to prevent the modal from opening when the select box is clic ...

Is it possible to retrieve the current CSS value set by a media query using JavaScript?

Currently working on a website project that involves accessing and manipulating the display property of a menu. The goal is to toggle the menu open or closed based on its current state. In my setup, the initial state of the menu is defined as closed using ...