简体   繁体   English

如何从包含相同数字的数组中获取所有索引值?

[英]How to get all index value from array including same numbers?

copyarray = yAxisdata.slice(0);  
length=yAxisdata.length;   
for (k = 0; k <length-1; k++)
{ 
     maxvalue = copyarray[k];
     indexvalue = yAxisdata.indexOf(maxvalue);
}

I am trying to find the index value of the data from array who's contain 800 data approximately and it is also contain lots of same value.

Above codes are used to get the index value of all the elements which is available inside the array.以上代码用于获取数组内所有可用元素的索引值。 but the problem is if array have a multiple same value at that time this code shows only index of first value and skip the same values in array and show the index of the other value.但问题是如果数组当时有多个相同的值,则此代码仅显示第一个值的索引并跳过数组中的相同值并显示另一个值的索引。

I'm not 100% familiar with javascript but i think below will do the trick我对 javascript 不是 100% 熟悉,但我认为下面会解决问题

copyarray = yAxisdata.slice(0);  
length=yAxisdata.length;  
var result;
var maxvalue = 0;
var counter;

for (k = 0; k <length-1; k++)
{ 
    maxvalue = copyarray[k];
    result = [];
    counter = 0;

    for(x = 0; x < length -1; x++)
    {
        if(copyarray[x] == maxvalue)
        {
            result[counter] = x;
            counter++;
        }
    }
}

The C# version would be C# 版本将是

int[] copyarray = yAxisdata.Select(ch => ch - '0').ToArray();

List<int> result = new List<int>();
int maxvalue = 0; 

for (int k = 0; k < copyarray.Count() - 1; k++)
{
    maxvalue = copyarray[k];
    
    // Alternative code
    // result = copyarray.Select((value, index) => new { value, index }).Where((a) => a.value == maxvalue).Select((a) => a.index).ToList();
    result = new List<int>();

    for(x = 0; x < copyarray.Count() - 1; x++)
    {
        if (copyarray[x] == maxvalue)
        {
            result.Add(x);
        }
    }
}

The result variable is now an array with all the indexes containing the maxvalue结果变量现在是一个数组,其中所有索引都包含最大值

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

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