如何返回数组中最长的字符串 - 即使数组包含不是字符串的项目?

时间:2015-06-30 05:25:02

标签: javascript arrays string sorting

假设我正在写一个这样的函数:

function longestString (someArray) {
    // code
}

如果someArray = ['word','longer phrase',['a','b','c'],1234567891011121314151617],我希望函数只返回数组中最长的字符串,并忽略整数和其他可能位于其中的数组。我试过这个:

function longestString (someArray) {
    return someArray.sort(function (a, b) { return b.length - a.length; })[0];
}

它不起作用,我现在卡住了。 :/

6 个答案:

答案 0 :(得分:4)

仅过滤字符串元素并执行

[HttpGet]
public ActionResult GetMyValues()
{
    List<MyTable> myList = myObj.GetValues();
    return PartialView("MyPartialView", myList);
}

答案 1 :(得分:0)

试试这个:

function longestString(someArray) {
    var result = "";
    for (var i = 0; i < someArray.length; i++) {
        if ((typeof someArray[i] === "string") && (someArray[i].length > result.length)) {
            result = someArray[i];
        }
    }
    return result;
}

答案 2 :(得分:0)

尝试做:

function longestString (someArray) {
$longest_string = '';
foreach ($someArray as $value)
    {
     $current_length = strlen($longest_string);
     if(strlen($value) > $current_length )  $longest_string = $value;
    }

    return $longest_string; 

}

答案 3 :(得分:0)

试试这个。

var a = ['word', 'longer phrase', ['a', 'b', 'c'], 1234567891011121314151617];

returnLongestString(a);

function returnLongestString(arr) {
	var longestString = '';
	for (var i = 0; i < arr.length; i++) {
		if (typeof arr[i] == "string" && arr[i].length > longestString.length) {
			longestString = arr[i];
		}
    };
	return longestString;
}

答案 4 :(得分:0)

继承我的态度。真的,你想知道的是这个短语是否比前一个短,如果它是一个字符串。

jsFiddle here.

var someArray = ['word','longer phrase',['a','b','c'],1234567891011121314151617]

var longestString = function(arr) {
    var longest = "";
    for (var i = 0; i < arr.length; i++) {
        var value = arr[i];
        if (typeof value === "string") {
            longest = arr[i];
        }
    }
    alert(longest);
}

longestString(someArray);

答案 5 :(得分:0)

当我看到这个问题时,它有0个答案,当我完成编写和测试我的功能时,有五个。但这是我的方法:

这里的关键是首先只过滤字符串。然后,列出阵列上的所有长度。然后使用该数组,您可以在较大的数组上获得匹配。

function longestString (someArray) {
//we only need strings, so first we will filter all the data
var stringsOnly = [];
for (var i = 0; i < someArray.length; i++) {
    if(typeof(someArray[i]) === 'string'){
        stringsOnly.push(someArray[i]);
    }
};
//Now with an array of just strings, we can get their indivial lenghts
var stringLengths = [];
for (var i = 0; i < stringsOnly.length; i++) {
    var currentString = stringsOnly[i];
    stringLengths.push(currentString.length);
};
//Get the max length
var maxLength = Math.max.apply(Math,stringLengths);
//get a string wich length equals to maxLength
for (var i = 0; i < stringsOnly.length; i++) {
    var theString = stringsOnly[i];
    if(theString.length === maxLength){return theString};
 }; 
}

此函数将返回最大的字符串。如果多个字符串具有相同的长度,它将返回第一个字符串。但是,如果你想获得各种字符串,你可以对上面的函数进行一些修改:

function multipleLongestString (someArray) {
//we only need strings, so first we will filter all the data
var stringsOnly = [];
for (var i = 0; i < someArray.length; i++) {
    if(typeof(someArray[i]) === 'string'){
        stringsOnly.push(someArray[i]);
    }
};
//Now with an array of just strings, we can get their indivial lenghts
var stringLengths = [];
for (var i = 0; i < stringsOnly.length; i++) {
    var currentString = stringsOnly[i];
    stringLengths.push(currentString.length);
};
//Get the max length
var maxLength = Math.max.apply(Math,stringLengths);

//modification here
longests = [];
//get a string wich length equals to maxLength
for (var i = 0; i < stringsOnly.length; i++) {
    var theString = stringsOnly[i];
    if(theString.length === maxLength){longests.push(theString)};
 };
return longests;
}

这是一个额外的,可能会帮助你以后。但如果您只想要最大的一个,请使用第一个函数。我希望我的回答是相关的。

:)