Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从具有嵌套数组的数组中获取最小字符串_Javascript - Fatal编程技术网

Javascript 从具有嵌套数组的数组中获取最小字符串

Javascript 从具有嵌套数组的数组中获取最小字符串,javascript,Javascript,我试图从以下数组对象中的每个嵌套数组中获取最小的字符串 let data = ["test string", ["abcd", "efj", ["hijklm", ["op"], "hijk", "hijklmn", ["op", "opq"]]]] 我已经尝试了代码,但它给了我stackoverflow错误,请任何

我试图从以下数组对象中的每个嵌套数组中获取最小的字符串

let data = ["test string", ["abcd", "efj", ["hijklm", ["op"], "hijk", "hijklmn", ["op", "opq"]]]]
我已经尝试了代码,但它给了我stackoverflow错误,请任何帮助

let data=[“测试字符串”、[“abcd”、“efj”、“hijklm”、“op”、“hijk”、“hijklmn”、“op”、“opq”]]
让最小的=[]
函数(数据){
data.forEach((ele,i)=>{
如果(元素类型==“字符串”){
最小推力(ele);
}else if(元素类型==“对象”){
//首先移除阵列
让_data=JSON.parse(JSON.stringify(data));
仅设_数组=_data.splice(i,1);
GetMinimest(仅适用于_数组)
//现在数据只包含字符串
//从数组中查找smalles字符串
let small=\u data.filter(v=>typeof v=='string')
.减少((a,v)=>a和&a.长度
let data=[“测试字符串”、[“abcd”、“efj”、“hijklm”、“op”、“hijk”、“hijklmn”、“op”、“opq”]]

const shorter=(left,right)=>left.length您可以采用递归方法

const
最小=数组=>数组
.减少((r,值)=>{
if(Array.isArray(value))r.push(…最小(value));
如果(!r[0].length | | r[0][0].length>value.length)r[0][0]=value;
返回r;
}, [[]])
.flat(),
数据=[“测试字符串”、[“abcd”、“efj”、[“hijklm”、[“op”]、“hijk”、“hijklmn”、[“op”、“opq”]],
结果=最小值(数据);

console.log(result);
您可以使用
.reduce
,下面是一个示例:

const data=[“测试字符串”、[“abcd”、“efj”、“hijklm”、“op”、“hijk”、“hijklmn”、“op”、“opq”]]
常量排序权重=(v)=>Array.isArray(v)?无穷大:v.length
常数减速机=(acc、cur、i、arr)=>{
if(Array.isArray(cur)){
acc=[…acc,…电流减少(减速机,[]);
}else如果(i==0){
常量smallesthislevel=arr.sort((a,b)=>{
a=分拣重量(a);
b=分拣重量(b);
返回a-b;
})[0];
如果(!Array.isArray(smallestAtThisLevel)){
acc.push(该级别的最小值);
}
}
返回acc;
}
const result=data.reduce(reducer,[]);

console.log(result);
具有递归函数和
reduce的解决方案

let data=[“测试字符串”、[“abcd”、“efj”、“hijklm”、“op”、“hijk”、“hijklmn”、“op”、“opq”]]
让结果=[];
函数getSmallestString(inp){
让最小的=inp.减少((a,v)=>{
if(数组isArray(v)){
getSmallestString(v);
返回a;
}
如果(!a | v.lengthconsole.log(result);
此版本的工作原理是将结果累积到一个数组中(最初为空),该数组通过递归层传递:

// (sup-optimal) helper function to split the array by type
// if you want optimal, use the underscore version
const partition = (a, pred) => [ a.filter(pred), a.filter(e => !pred(e)) ];

// helper function for reduce
const shorter = (a, b) => (a.length < b.length) ? a : b;

function getSmallest(data, result = []) {

  // split the array into strings and subarrays
  const [strings, sub] = partition(data, e => typeof e === 'string');

  // add the shortest string, if any
  if (strings.length) {
    result.push(strings.reduce(shorter));
  }

  // recurse through sub-arrays, if any
  if (sub.length) {
    sub.forEach(e => getSmallest(e, result));
  }

  return result;
}
/(sup optimal)helper函数按类型拆分数组
//如果希望达到最佳效果,请使用下划线版本
常量分区=(a,pred)=>[a.filter(pred),a.filter(e=>!pred(e));
//reduce的helper函数
常数较短=(a,b)=>(a.长度typeof e=='string');
//添加最短的字符串(如果有)
if(strings.length){
结果.push(字符串.reduce(较短));
}
//通过子数组递归(如果有)
如果(子长度){
sub.forEach(e=>getminimate(e,result));
}
返回结果;
}

只是普通的javascript

let数据=[
“测试字符串”,
[“abcd”、“efj”、“hijklm”、“op”、“hijk”、“hijklmn”、“op”、“opq”]],
];
设k=0;
让结果=[];
函数smallestStringSolve(arr){
设temp_arr=[];
设最小字符串为无穷大;
让最小的_字符串=”;
for(设i=0;i对于(让i=0;我请添加想要的结果。多个最短字符串会发生什么情况?为什么
最小的
是一个数组?不是只有一个最小的字符串吗?让
数据=JSON.parse(JSON.stringify(data));
的目标是什么?(
JSON.parse(JSON.stringify(x))
在99.99999%的时间里是反模式。)你在代码中使用了很多过于复杂的东西。只需对
使用简单的
,或对
循环使用
。对于那些建议展平数组的人,OP看起来确实想要一个展平数组结果,但值是在每一层嵌套数组中找到的最短字符串。