admin管理员组文章数量:1391999
I am trying to flatten an array of arrays in a input, and return the longest string.
For example given the input:
i = ['big',[0,1,2,3,4],'tiny']
The function should return 'tiny'. I would like use reduce or concat for resolve this in a native and elegant way (without implement a flatten prototype in array) but I am failing with this code:
function longestStr(i)
{
// It will be an array like (['big',[0,1,2,3,4],'tiny'])
// and the function should return the longest string in the array
// This should flatten an array of arrays
var r = i.reduce(function(a, b)
{
return a.concat(b);
});
// This should fetch the longest in the flattened array
return r.reduce(function (a, b)
{
return a.length > b.length ? a : b;
});
}
I am trying to flatten an array of arrays in a input, and return the longest string.
For example given the input:
i = ['big',[0,1,2,3,4],'tiny']
The function should return 'tiny'. I would like use reduce or concat for resolve this in a native and elegant way (without implement a flatten prototype in array) but I am failing with this code:
function longestStr(i)
{
// It will be an array like (['big',[0,1,2,3,4],'tiny'])
// and the function should return the longest string in the array
// This should flatten an array of arrays
var r = i.reduce(function(a, b)
{
return a.concat(b);
});
// This should fetch the longest in the flattened array
return r.reduce(function (a, b)
{
return a.length > b.length ? a : b;
});
}
Share
Improve this question
asked Oct 4, 2013 at 0:52
shakaranshakaran
11.1k3 gold badges32 silver badges47 bronze badges
5
- 10 I don't think you can JavaScript under pressure ... – Pointy Commented Oct 4, 2013 at 0:55
- Why don't you just work recursively instead of flattening the array? – n.st Commented Oct 4, 2013 at 0:58
- @Poinly yep, I fail in level 4 :) – shakaran Commented Oct 4, 2013 at 1:01
- @n.st I try recursive for solve the problem and It works, but previously I cannot get with this reduce/contact solution, so I think that someone can help me – shakaran Commented Oct 4, 2013 at 1:03
- I just noticed that the task does not in fact require you to recurse into any sub-arrays. – n.st Commented Oct 4, 2013 at 1:12
2 Answers
Reset to default 7Your issue is that you forgot to pass in the initialValue
argument to the reduce function, which must be an array in this case.
var r = i.reduce(function(a, b) {
return a.concat(b);
}, []);
Without providing an initialValue
, the a
value for the first call will be the first element in the i
array, which is the string big in your case, so you will be calling the String.prototype.concat
function instead of Array.prototype.concat
.
That means at the end, r
is a string and strings don't have a reduce
function.
Your solution could be simplified however:
['big',[0,1,2,3],'tiny'].reduce(function longest(a, b) {
b = Array.isArray(b)? b.reduce(longest, '') : b;
return b.length > a.length? b : a;
}, '');
You don't mention having multiple strings with the same length- or if you care about IE8...
function longestStr(A){
var i= 0, len, A= String(A).split(/\b/).sort(function(a, b){
return a.length<b.length;
});
len= A[0].length;
while(A[i].length==len)++i;
return A.slice(0, i);
}
var A1= ['big', [0, 1, 2, 3, 4], 'tiny',[1,2,3,'puny']];
longestStr(A1);
/* returned value: (Array)
tiny,puny
*/
Method 2:
You did not define a string as a single word-
any array delimeters could be included in any of the values, making my solution incorrect.
Flattening the array makes paring each item's length simple-
and it does not have to be done as a prototype method:
function longestStr(array){
function flatten(arr){
var A1= [], L= arr.length, next;
for(var i= 0; i<L; i++){
next= arr[i];
if(next.constructor!= Array) A1.push(String(next));
else A1= A1.concat(flatten(next));
}
return A1;
}
var i= 0, len, A=flatten(array);
A.sort(function(a, b){
return a.length<b.length;
});
len= A[0].length;
while(A[i].length== len)++i;
return A.slice(0, i);
}
var Ax= ['big stuff', [0, 1, 2, 3, 4], 'tiny', [1, 2, 3, 'puny']];
longestStr(Ax);
/* returned value: (Array)
big stuff
*/
本文标签: Flatten an array of arrays in javascript for get the longest stringStack Overflow
版权声明:本文标题:Flatten an array of arrays in javascript for get the longest string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744590654a2614469.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论