简体   繁体   中英

Finding the longest strings within a multidimentional array in JavaScript

 let lists = ["Grocery", "Clothing", "Furniture"]; let items = [ [ "tomatoes", "cheese", "bread", "ham" ], [ "shirt", "jacket", "jeans" ], [ "sofa", "carpet", "bed" ] ]; 

So I have these two arrays. one in which the 'items' arrays belong to each array of lists. for example items[0] belongs to lists[0] etc.

to try to get the longest string from the arrays i tried this but it doesn't work....

 let longestString = (list) => { lists[items.reduce((a, b) => a.length > b.length ? a : b)]; } console.log('Clothing's longest item is: ${longestString("Clothing")}`) 

This is the method you can use to ding the longest string in an array

function findLongest(array) {
  var currentMax = "";
  for (string of array) {
      if (string.length > currentMax.length) {
          currentMax = string
      }
  }
  return currentMax;
}

then, you can use this function on each of the arrays in "items". I'll leave that one to you ^^

You could map the array by taking an array of strings as result set.

 var lists = ["Grocery", "Clothing", "Furniture"], items = [["tomatoes", "cheese", "bread", "potatoes"], ["shirt", "jacket", "jeans"], ["sofa", "carpet", "bed"]], result = items.map(a => a.reduce((r, v) => { if (!r || r[0].length < v.length) { return [v]; } if (r[0].length === v.length) { r.push(v); } return r; }, undefined)); lists.forEach((l, i) => console.log(`${l}'s longest item${result[i].length === 1 ? ' is' : 's are'} ${result[i].join(', ')}.`)); console.log(result); 

 let lists = ["Grocery", "Clothing", "Furniture"]; let items = [ [ "tomatoes", "cheese", "bread", "potatoes" ], [ "shirt", "jacket", "jeans" ], [ "sofa", "carpet", "bed" ] ]; var listsObj = lists.reduce( (obj, k, idx) => { obj[k] = items[idx].reduce( (res, i) => (res.length > i.length ? res : i), '' ); return obj; }, {} ); lists.forEach(k => console.log(`${k}'s longest item is: ${listsObj[k]}.`)); 

Hope this helps you!

You need to first find the index in lists that corresponds to the given list , and then apply the reduce call on that particular sub array:

 const lists = ["Grocery", "Clothing", "Furniture"]; const items = [["tomatoes", "cheese", "bread", "potatoes"], ["shirt", "jacket", "jeans"], ["sofa", "carpet", "bed"]]; const longestString = (list) => items[lists.indexOf(list)].reduce((a, b) => a.length > b.length ? a : b); console.log(`Clothing's longest item is: ${longestString("Clothing")}`) 

It would however be a better data structure if you would store the list name and the corresponding items together :

 const lists = { Grocery: ["tomatoes", "cheese", "bread", "potatoes"], Clothing: ["shirt", "jacket", "jeans"], Furniture: ["sofa", "carpet", "bed"] }; const longestString = (list) => lists[list].reduce((a, b) => a.length > b.length ? a : b); console.log(`Clothing's longest item is: ${longestString("Clothing")}`) 

try below code snippet

 let lists = ["Grocery", "Clothing", "Furniture"]; let items = [ [ "tomatoes", "cheese", "bread", "ham" ], [ "shirt", "jacket", "jeans" ], [ "sofa", "carpet", "bed" ] ]; lists.forEach( (category, index) => { let longest = items[index].reduce(function (a, b) { return a.length > b.length ? a : b; }); console.log(`${category}'s longest item is: ${longest}`); }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM