简体   繁体   English

为什么我的函数返回不同的数组?

[英]Why does my function return a different array?

I have some code which is meant to do the following: 我有一些代码打算执行以下操作:

Given two arrays of strings a1 and a2 return a sorted array r in lexicographical order of the strings of a1 which are substrings of strings of a2 . 给定字符串a1a2两个数组,按a1的字符串的字典顺序返回排序的数组r ,它们是a2的字符串的子字符串。
Arrays are written in "general" notation. 数组以“通用”符号表示。

Now I'm doing a test where array1 = ["arp", "live", "strong"] and array2 = ["lively", "alive", "harp", "sharp", "armstrong"] . 现在,我正在做一个测试,其中array1 = ["arp", "live", "strong"]array2 = ["lively", "alive", "harp", "sharp", "armstrong"] I think I've got it, but I don't understand why the function only returns the array ["arp", "strong"] when in the last for loop I replace newArray.splice(l, l+1) with newArray.splice(k, k+1) . 我想我已经知道了,但我不明白为什么函数只返回数组["arp", "strong"]在过去的时候for循环替换我newArray.splice(l, l+1)newArray.splice(k, k+1) Could anyone tell me why that is? 谁能告诉我为什么呢?

 function inArray(array1, array2) { var newArray = []; var sortedArray = []; for (var i in array2) { for (var j in array1) { if (array2[i].includes(array1[j])) { newArray.push(array1[j]); }; }; }; sortedArray = newArray.sort(); for (var k = 0; k < newArray.length; k++) { for (var l = 0; l < newArray.length; l++) { if (newArray[k] === newArray[l] && k != l) { newArray.splice(l, l + 1) } } } return sortedArray; }; console.log(inArray(["arp", "live", "strong"], ["lively", "alive", "harp", "sharp", "armstrong"])); 

The reason is that the second argument to splice() isn't the end position of the splice, but the count of elements that should be removed. 原因是splice()的第二个参数不是splice()的结束位置,而是应删除的元素数。 So when k = 1 , you removed 2 elements, not just the element at index 1 . 因此,当k = 1 ,您删除了2个元素,而不仅仅是索引1的元素。

Both newArray.splice(l, 1) and newArray.splice(k, 1) work correctly. newArray.splice(l, 1)newArray.splice(k, 1)可以正常工作。

 function inArray(array1,array2){ var newArray = []; var sortedArray = []; for (var i in array2) { for (var j in array1) { if (array2[i].includes(array1[j])) { newArray.push(array1[j]); }; }; }; sortedArray = newArray.sort(); for (var k = 0; k < newArray.length; k++) { for (var l = 0; l < newArray.length; l++) { if (newArray[k] === newArray[l] && k != l) { newArray.splice(k, 1); } } } return sortedArray; }; console.log(inArray(["arp", "live", "strong"], ["lively", "alive", "harp", "sharp", "armstrong"])); 

In the comments already you get the answer. 在评论中,您已经得到了答案。 Use ES2015/ES6 always you can, benefit of it's adventages. 借助它,您可以始终使用ES2015 / ES6。

 const words1 = ["arp", "live", "strong"]; const words2 = ["lively", "alive", "harp", "sharp", "armstrong"]; const final = []; words1.forEach(v => { words2.forEach(v2 => { if (v2.includes(v)) { final.push(v); } }); }); const finalSorted = [...new Set(final.sort())]; // just to add html const pre = document.getElementById('result'); finalSorted.forEach(v => { let content = pre.textContent; content += `\\n- ${v}`; pre.textContent = content; }); 
 <label>Result:</label> <pre id="result"></pre> 

Looks like you're overcomplicating it a bit ;) 看起来您有点复杂了;)

 a1 = ["arp", "live", "strong", "bazooka"] a2 = ["lively", "alive", "harp", "sharp", "armstrong"] result = a1 .filter(x => a2.some(y => y.includes(x))) .sort(); console.log(result); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM