Find the shortest string in array

Michael Shtefanitsa

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].length);

and i have result like shortest string between 3 elements of array. But I don't want to care about numbers of elements

Ori Drori

Use Array#map to create an array of lengths, and then apply it to Math.min():

var arr = ['cats', 'giants', 'daughters', 'ice'];
var min = Math.min.apply(Math, arr.map(function(str) { return str.length; }));
console.log(min);

Or use ES6's array spread and arrow function:

var arr = ['cats', 'giants', 'daughters', 'ice'];
var min = Math.min(...arr.map(({ length }) => length));
console.log(min);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Javascript reduce() to find the shortest word in a string

分類Dev

Shortest Distance From An Array

分類Dev

find 3 shortest distances in a matrix

分類Dev

How to find the random value for a particular string in an array

分類Dev

How to find duplicate elements in a string array and store the result in another array

分類Dev

how to find shortest path between multiple clusters

分類Dev

Find shortest path to one of many nodes with attribute

分類Dev

Find shortest path to one of many nodes with attribute

分類Dev

How to find shortest path in skeletonized maze image?

分類Dev

Find Maximum of Array of Index/Match from Concatenated String

分類Dev

Find Numeric Values among string from Array of Objects and do calculation

分類Dev

Find shortest distance, while visiting every node exactly once

分類Dev

Using SciPy's minimize to find the shortest path in a graph

分類Dev

string to array?

分類Dev

How to find longest prefix from array list using given string value in Java?

分類Dev

Avoid using nested loops to find the max-sized sub-string of an array?

分類Dev

boost dijkstra_shortest_paths: can't extract (or find?) the path (path contains a cycle)

分類Dev

R igraph: find total number of shortest paths between nodes u and v that pass through node g

分類Dev

I need help adjusting an algorithm with a list of package deliveries to find the shortest path

分類Dev

regex: find string ending with * or?

分類Dev

Find the object with the longest string

分類Dev

To find a substring in a given string

分類Dev

Find multiple occurances in string

分類Dev

Find numbers in a string

分類Dev

Find String Pattern in Java

分類Dev

Find Certain String Indices

分類Dev

Find if string contains word

分類Dev

Find an object in array?

分類Dev

Find missing element in an array

Related 関連記事

  1. 1

    Javascript reduce() to find the shortest word in a string

  2. 2

    Shortest Distance From An Array

  3. 3

    find 3 shortest distances in a matrix

  4. 4

    How to find the random value for a particular string in an array

  5. 5

    How to find duplicate elements in a string array and store the result in another array

  6. 6

    how to find shortest path between multiple clusters

  7. 7

    Find shortest path to one of many nodes with attribute

  8. 8

    Find shortest path to one of many nodes with attribute

  9. 9

    How to find shortest path in skeletonized maze image?

  10. 10

    Find Maximum of Array of Index/Match from Concatenated String

  11. 11

    Find Numeric Values among string from Array of Objects and do calculation

  12. 12

    Find shortest distance, while visiting every node exactly once

  13. 13

    Using SciPy's minimize to find the shortest path in a graph

  14. 14

    string to array?

  15. 15

    How to find longest prefix from array list using given string value in Java?

  16. 16

    Avoid using nested loops to find the max-sized sub-string of an array?

  17. 17

    boost dijkstra_shortest_paths: can't extract (or find?) the path (path contains a cycle)

  18. 18

    R igraph: find total number of shortest paths between nodes u and v that pass through node g

  19. 19

    I need help adjusting an algorithm with a list of package deliveries to find the shortest path

  20. 20

    regex: find string ending with * or?

  21. 21

    Find the object with the longest string

  22. 22

    To find a substring in a given string

  23. 23

    Find multiple occurances in string

  24. 24

    Find numbers in a string

  25. 25

    Find String Pattern in Java

  26. 26

    Find Certain String Indices

  27. 27

    Find if string contains word

  28. 28

    Find an object in array?

  29. 29

    Find missing element in an array

ホットタグ

アーカイブ