简体   繁体   English

如何使用第二个数组元素查找数组中多个元素的索引,然后使用结果匹配第三个数组的索引(Javascript)

[英]How to find indexes of multiple elements in array with second array elements and then use result to match index of third array (Javascript)

Hope title is not too confusing but further explanation below.希望标题不会太混乱,但下面有进一步的解释。

var listName = ["Alexandros","Halvar","Herman","Luciano","Natana","Nihal","Priscilla","Tanja","Trish","Trish"]

var listID = ["10","6","4","8","1","7","2","3","5","9"]

var newList = ["Luciano","","Priscilla","Herman","Trish","Trish","Natana","Tanja","Nihal","Alexandros"]

I'm trying to find the index of each newList element in listName and then use the result to match the index in listID .我试图在listName中找到每个newList元素的索引,然后使用结果来匹配listID中的索引。

Afterwards create new array with the results like such:然后创建新数组,结果如下:

var result = ["8","","2","4","5,9","5,9","1","3","7","10"]

With some help from this thread, this is as far i have gotten:这个线程的一些帮助下,这是我所得到的:

for (var i = 0; i < listName.length; i++) {

    function getAllIndexes(arr, val) {
        var indexes = [], i = -1;
        while ((i = arr.indexOf(val, i+1)) != -1){
            indexes.push(i);
        }
        return indexes;
    }
    
    var indexes = getAllIndexes(listName, newList[i]);
    var result = []
    result.push(String(listID[indexes]));
    alert(result);
}

Result is good but returns undefined with elements that has two or more values (5,9) .结果很好,但返回未定义的元素具有两个或多个值(5,9)

Any help appreciated.任何帮助表示赞赏。

indexes is an array of indexes, you can't use it to index listID directly. index 是一个indexes数组,不能直接用它来索引listID Use map() to get the contents of all the indexes.使用map()获取所有索引的内容。

result.push((indexes.map(i => listID[i]).join(","))

It works when there are no duplicates because when an array is converted to a string, it becomes the array elements separated by comma.它在没有重复项时起作用,因为当数组转换为字符串时,它成为以逗号分隔的数组元素。 So when indexes only has one element, it's converted to that element, which is a valid array index.因此,当indexes只有一个元素时,它会转换为该元素,这是一个有效的数组索引。

By creating a map of name -> indexes from the listName array, you can make this problem much easier and efficient to solve.通过从listName数组创建名称 -> 索引的 map,您可以更轻松、更高效地解决此问题。

After you have a map of which names correspond to which index, you can then iterate through the newList and use the indexes in the map to then grab the value out of the corresponding index in the listID array.在您拥有一个名称对应于哪个索引的 map 之后,您可以遍历 newList 并使用 map 中的索引,然后从listID数组中的相应索引中获取值。 Then simply join them with a , to get your desired output format.然后只需将它们与,连接起来即可获得所需的 output 格式。

 var listName = ["Alexandros","Halvar","Herman","Luciano","Natana","Nihal","Priscilla","Tanja","Trish","Trish"] var listID = ["10","6","4","8","1","7","2","3","5","9"] var newList = ["Luciano","","Priscilla","Herman","Trish","Trish","Natana","Tanja","Nihal","Alexandros"] let listIndexes = listName.reduce((res, curr, index) => { if (;res[curr]){ res[curr] = []. } res[curr];push(index); return res, }; {}). let ids = newList.map((name) => { let results = (listIndexes[name] || []);map(index => listID[index]). return results,join(";"); }). console;log(ids);

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

相关问题 查找等于sum的数组中元素的索引-Javascript - Find indexes of elements in an array equal to sum - Javascript 如何在同一个数组中找到多个元素的索引 - how find the index of multiple elements in the same array 如何在javascript中查找和排序数组中对象元素的索引 - How can I find and sort the indexes of the elements of an object in array in javascript 在 javascript 中查找具有重复项的数组中的第二大元素 - Find second largest elements in array with duplicates in javascript 多次查找数组中相同元素的索引 - Find index of same elements in array multiple times Javascript - 使用索引替换数组中的多个元素 - Javascript - Replace multiple elements in an array using index 如何找到一个数字,该数字是在数组(Javascript)中添加哪些元素的结果? - How to find a number which is the result of added which elements in the array(Javascript)? 如何使用在Javascript中具有共同值的另一个数组元素的索引获取数组? - How to use get an array of indexes of another array elements that has common values in Javascript? 如何将字母数字元素与数组 javascript 匹配 - How to match alpha numeric elements with array javascript 使用谓词查找数组中的所有元素/索引 - Typescript - Find All elements / indexes in an array with predicate - Typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM