Find the shortest string in array

后端 未结 5 1606
遇见更好的自我
遇见更好的自我 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:51

    Please see this short solution below. I hope it helps:

    var arr = ['cats', 'giants', 'daughters', 'ice'];    
    arr.sort(); // ice, cats, giants, daughters
    var shortest_element = arr[0]; // ice
    

提交回复
热议问题