从数组中获取最小的数组 [英] Get the smallest array from an array of arrays

查看:49
本文介绍了从数组中获取最小的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组数组,我想从 paths array

I have an array of arrays, I want to get the smallest (shortest) path from paths array

paths = [ 
   ["LEFT", "RIGHT", "RIGHT", "BOTTOM", "TOP"],
   ["RIGHT", "LEFT", "TOP"],
   ["TOP", "LEFT"]
];

paths.map((path)=> Math.min(path.length));

推荐答案

使用 Array#reduce 方法.

var paths = [
  ["LEFT", "RIGHT", "RIGHT", "BOTTOM", "TOP"],
  ["RIGHT", "LEFT", "TOP"],
  ["TOP", "LEFT"]
];

console.log(paths.reduce((prev, next) => prev.length > next.length ? next : prev))

这篇关于从数组中获取最小的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆