Find the shortest string in array

后端 未结 5 1607
遇见更好的自我
遇见更好的自我 2020-12-10 21:03

How can i find the shortest string in javascript array with different count of array elements? I used

var min = Math.min(arr[0].length,arr[1].length,arr[2]         


        
5条回答
  •  余生分开走
    2020-12-10 21:34

    You could use Array.prototype.reduce

    const arr = ['small', 'big', 'yuge']
    
    const shorter = (left, right) => left.length <= right.length ? left : right
    
    console.log(
      arr.reduce(shorter)
    )

提交回复
热议问题