[英]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.
例如,项[0]属于列表[0]等。
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 这是您可以用来对数组中最长的字符串进行ding的方法
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: 你需要先找到在索引
lists
对应于给定的list
,然后再应用reduce
特定子阵列上的呼叫:
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}`); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.