How to return string attached to largest integer in an array (Javascript)

Anton

So I have a function that takes in an array of objects with the parameters of (name, traffic) where name is a String and traffic is an int. e.g [{"Monday",12},{"Tuesday",10}] The function then is supposed to return the name/names which have the highest traffic value

However I can't figure out how to return either just the name associated to the object e.g "Monday", or how to return multiply names in an array for objects that share the highest traffic. e.g ["Monday", "Tuesday"]

Here's my code so far;

function mostPopularDays(week) {
 if (week == null || week == []){
  return null;
 }
 /*just so it returns null if nothing is given*/
 var arr = [];
 var largest = week[0];
 for (var i = 0; i<week.length; i++){
   if (largest < week[i]){
    largest = week[i]
      }
    }
 arr.push(largest);
 /*takes the largest value and puts it in an array*/
 for (var i = 0; i<week.length; i++){
   if (week[i.] == largest){
    arr.push(week[i]);
     }
   }
 /*takes values that have the same amount and adds them to the array*/
 if(arr.length = 1){
 return arr[0]
   }
 /*returns the object if the array has just one entry*/
 return arr;
 }
dhaker

You need to pass objects as key value pair inside array.

function mostPopularDays(week) {
 if (week == null || week == []){
  return null;
 }
 /*just so it returns null if nothing is given*/
 var arr = [];
 var largest = week[0].traffic;
 for (var i = 0; i<week.length; i++){
   if (largest < week[i].traffic){
    largest = week[i].traffic
      }
 }

 /*takes the largest value and puts it in an array*/
 for (var i = 0; i<week.length; i++){
   if (week[i].traffic == largest){
    arr.push(week[i].name);
    }
 }
 
if(arr.length==1)
{
  arr = arr[0];
}
 
return arr; 
}

var obj1 = [{name:"Monday",traffic:10},{name:"Sunday",traffic:20},{name:"Wednesday",traffic:22}];

//will return string 
console.log(mostPopularDays(obj1));

var obj2 = [{name:"Monday",traffic:10},{name:"Sunday",traffic:20},{name:"Wednesday",traffic:20}];

//will return array
console.log(mostPopularDays(obj2));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Javascript - How to return the largest four numbers in an array?

How to return the largest integer in an Array that has 10 random integers in it?

JavaScript Return Array Item(s) With Largest Score

Return Largest Number in an Negative Array. [Javascript]

Return integer from string in JavaScript

How to return an integer in a %u string?

Is JavaScript array index a string or an integer?

Convert array of string javascript to array of integer javascript

How to create an integer or number from array of digits or string of digits in JavaScript

Return Largest 3-Digit Chunk From String - JavaScript

How to return a String after Integer/String manipulation?

given an array A of N integers, return the largest integer K>0 such that both K and -K(the opposite number) exist in array A

How to use an INTEGER value to perform a lookup into a STRING array in a SQL SELECT statement and return the STRING

convert integer array to string array in javascript

How to change String to Integer in array

How to convert a string with "[]" into an integer array

Return longest string in array (JavaScript)

How do I return the sum of the three largest elements in an array?

JS How to take an array of numbers and return the largest passible combined number

How to return the indexes that tell the position of largest to smallest integers of an array in Ruby

How to return array element whose first index is the largest number in JS?

return the largest array between arrays

String with Comma separated numbers to array of integer in javascript

Convert string into integer inside array in javascript

Javascript - push String to Array returns Integer

How to convert String of array to Integer array

javascript - search for string in array of objects and return array

How to get each array of String with the largest number and with an unique key?

Printing a HashMap String value with largest Integer Key