我看到了各种各样的答案,这些答案为找到数组中最长的字符串提供了解决方案。我的问题是,我想在嵌套数组中找到最长的字符串。嵌套级别可以是N个级别,也可以只有两个级别。我最初的解决方法如下:
let myArray = [
'ABC',
'ABCD',
'ABCDE',
[
'ABC',
'ABCABABA',
[
'ABABABABABABABABAZZ'
],
'ABCABABASS',
],
'ABCDEFGH',
'ABABABABZZQ'
]
function longestString(arr) {
let longestStr = ''
arr.forEach(item => {
if(typeof item === 'string') {
if(item.length > longestStr.length) {
longestStr = item;
console.log('Longest Item', item);
}
} else {
longestString(item)
}
})
return longestStr;
}
console.log(longestString(myArray))
观察到的输出-> ABABABABZZQ
预期输出->'ABABABABABABABABAZZ'
仅打印最长的字符串需要进行哪些调整?
答案 0 :(得分:2)
您需要使用递归调用的返回值-将返回的字符串通过与放置item
相同的测试来放置,检查它是否长于longestStr
,如果是,则重新分配longestStr
:
let myArray = [
'ABC',
'ABCD',
'ABCDE', [
'ABC',
'ABCABABA', [
'ABABABABABABABABAZZ'
],
'ABCABABASS',
],
'ABCDEFGH',
'ABABABABZZQ'
]
function longestString(arr) {
let longestStr = ''
arr.forEach(item => {
if (typeof item === 'string') {
if (item.length > longestStr.length) {
longestStr = item;
}
} else {
const nestedLongest = longestString(item);
if (nestedLongest.length > longestStr.length) {
longestStr = nestedLongest;
}
}
})
return longestStr;
}
console.log(longestString(myArray))
或者,更干燥:
const myArray=['ABC','ABCD','ABCDE',['ABC','ABCABABA',['ABABABABABABABABAZZ'],'ABCABABASS',],'ABCDEFGH','ABABABABZZQ']
function longestString(arr) {
let longestStr = '';
const check = str => {
if (str.length > longestStr.length) longestStr = str;
};
arr.forEach(item => {
check(typeof item === 'string' ? item : longestString(item));
});
return longestStr;
}
console.log(longestString(myArray))
另一种选择是有一个内部函数被调用,同时分配一个在范围内持续存在的longestStr
变量,直到longestString
函数结束-这意味着您不必担心递归调用的结果:
const myArray=['ABC','ABCD','ABCDE',['ABC','ABCABABA',['ABABABABABABABABAZZ'],'ABCABABASS',],'ABCDEFGH','ABABABABZZQ']
function longestString(input) {
let longestStr = '';
const check = str => {
if (str.length > longestStr.length) longestStr = str;
};
function recursiveFn(arr) {
arr.forEach((item) => {
if (typeof item === 'string') check(item)
else recursiveFn(item);
});
}
recursiveFn(input);
return longestStr;
}
console.log(longestString(myArray))
答案 1 :(得分:1)
使用Array.flat()
展平数组,然后使用Array.reduce()
查找最长的项:
const myArray = [
'ABC',
'ABCD',
'ABCDE', [
'ABC',
'ABCABABA', [
'ABABABABABABABABAZZ'
],
'ABCABABASS',
],
'ABCDEFGH',
'ABABABABZZQ'
]
const result = myArray
.flat(Infinity)
.reduce((r, s) => s.length > r.length ? s : r);
console.log(result);
答案 2 :(得分:1)
您必须在函数外部声明longestStr
,还必须使用return
关键字
return longestString(item)
let myArray = [['ABC','ABCD','ABCDE',
['ABC','ABCABABA',
['ABABABABABABABABAZZ'],
'ABCABABASS'],
'ABCDEFGH',
'ABABABABZZQ']]
let longestStr = ''
function longestString(arr) {
arr.forEach(item => {
if(typeof item === 'string') {
if(item.length > longestStr.length) {
longestStr = item;
console.log('Longest Item', item);
}
} else {
return longestString(item)
}
});
return longestStr;
}
console.log(longestString(myArray));
编辑-您可以将longestStr
作为参数传递给longestString
函数
let myArray = [['ABC','ABCD','ABCDE',
['ABC','ABCABABA',
['ABABABABABABABABAZZ'],
'ABCABABASS'],
'ABCDEFGH',
'ABABABABZZQ']]
function longestString(arr,longestStr) {
arr.forEach(item => {
if(typeof item === 'string') {
if(item.length > longestStr.length) {
longestStr = item;
console.log('Longest Item', item);
}
} else {
var value = longestString(item,longestStr);
if(value.length > longestStr.length){
longestStr = value;
}
return longestStr;
}
});
return longestStr;
}
console.log(longestString(myArray,''));
答案 3 :(得分:1)
您可以使用一次reduce
const myArray = [
'ABC',
'ABCD',
'ABCDE', [
'ABC',
'ABCABABA', [
'ABABABABABABABABAZZ'
],
'ABCABABASS',
],
'ABCDEFGH',
'ABABABABZZQ'
];
const findLongestStr = array => array.reduce((result, item) => typeof item === 'string' ? item.length > result ? item : result : findLongestStr(item), '');
console.log(findLongestStr(myArray));
答案 4 :(得分:0)
如果输入为findLongestStr
,则递归调用Array
并保持当前最大值。
let myArray = [
'ABC',
'ABCD',
'ABCDE',
[
'ABC',
'ABCABABA',
[
'ABABABABABABABABAZZ'
],
'ABCABABASS',
],
'ABCDEFGH',
'ABABABABZZQ'
];
function findLongestStr(input){
return input.reduce(function(o,i){
if(i.constructor === Array){
var value = findLongestStr(i);
console.log("value array => ",value);
if(o.length < value.length){
o = value;
}
}else{
console.log("value i => ",i);
if(o.length < i.length){
o = i;
}
}
return o;
},"");
}
console.log("max length => ",findLongestStr(myArray));
正在工作的jsFiddle演示-https://jsfiddle.net/k4yo35hb/1/
答案 5 :(得分:0)
您的代码的主要问题是每次调用递归函数时都要重新定义if (i%j) != 0:
。让您无法比较每个递归调用中最长的字符串。
使用longestStr
是查找常规未嵌套数组的最大值的好方法。您可以添加一点递归,并仍然使用reduce()
来维护递归之间的状态,因为所有递归都在reduce()
函数中展开:
reduce
答案 6 :(得分:0)
关于@Adrian Brand干净答案,这是解决方案:
const findMaxStringInArray = (array, lastMax = '' ) => array.reduce((curMax,item)=>typeof item === 'string'? item.length > curMax.length? item : curMax : findMaxStringInArray(item, curMax) ,lastMax);