Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.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_Arrays - Fatal编程技术网

Javascript 从阵列中删除重复项(了解代码)

Javascript 从阵列中删除重复项(了解代码),javascript,arrays,Javascript,Arrays,我正在看一个练习,很难理解下面的工作原理(我正在尝试从数组中删除重复项) 我的理解尝试 这里,项接受我们在arr中的所有值。让我们进行一次迭代 First item='a'和pos=0。好啊现在我们只想根据'a'的索引是否与0 此处indexOf(a)==0 太好了!这是真的,让我们把它放到新数组中 现在让我们继续前进,再次看到a,即在pos=3 arr.indexOf(a) == 3 等等。。。这也符合我们的要求吗?这是如何删除重复项的?indexOf只返回一个整数值,它是第一个找到的项的索

我正在看一个练习,很难理解下面的工作原理(我正在尝试从数组中删除重复项)

我的理解尝试

这里,
接受我们在
arr
中的所有值。让我们进行一次迭代
First item='a'
pos=0
。好啊现在我们只想根据
'a'
的索引是否与
0

此处
indexOf(a)==0

太好了!这是真的,让我们把它放到新数组中

现在让我们继续前进,再次看到a,即在
pos=3

arr.indexOf(a) == 3

等等。。。这也符合我们的要求吗?这是如何删除重复项的?

indexOf
只返回一个整数值,它是第一个找到的项的索引。因此,当
pos
为3(而
a
)时,
indexOf
将返回0(因为
a
的第一个索引为0),
0==3
为false,元素将被删除

然后,当
pos
为4(并且
b
)时,
indexOf
返回2,即第一个找到的
b
的索引


至于对象,它们不能有重复的关键点。每个新密钥将自动覆盖旧密钥,因此不会有任何重复项

看:

varobj={a:1,a:3,b:2,c:5,b:4};

console.log(obj)
nicael是对的
indexOf(item)
只是一个函数,它遍历数组,查找第一次出现在数组中的
item
,并返回数组中的位置。在您的示例中,如果在0处存在
a
,在索引3处存在
a
,则
indexOf('a')
将返回位置0,而
pos
的值为3,因此过滤器返回false

后续行动:

indexOf()
还有一个名为fromIndex的参数,它允许从数组开头以外的位置开始搜索。在这种情况下,您可以通过执行
arr.indexOf('a',1)
来指定跳过第一次出现
'a'
,该操作从位置1而不是0开始搜索。在这种情况下,函数将返回true,因为下一个
'a'
位于位置3

我可以对对象使用过滤器吗?

否,因为筛选器是数组对象的特定函数。通过对
object.keys(myObject)
since keys()返回数组,可以获得对象的键

以您的例子:

var keyArray = Object.keys(myObject); //object can't have duplicate keys

keyArray.filter(function(item, index) {

    return keyArray.indexOf(item) == index; //will never be false 

});

哈希表是消除冗余值的最佳方法 代码如下:

char arr[] = ['a','b','c','a','b','d','e','f'];
//it will contains all 26 places as zero (A - Z)
char hashArray[26]={0} 
int i; //for iteration;

for(i=0;arr[i]!='\0';i++)
{
  //it will subtracte the ascii value of the letter 
  //from 'a' so that we have the values from 0 to 26)
  hashArray[arr[i]-'a']=arr[i]; 
}

for(i=0;i<26;i++)
{
   if(hashArray[i]!=0) //to Ensure the positon has the character
   {
      printf("%c",hashArray[i]); 
   } 
}
chararr[]=['a','b','c','a','b','d','e','f'];
//它将包含所有26个零位(A-Z)
字符哈希数组[26]={0}
int i//用于迭代;
对于(i=0;arr[i]!='\0';i++)
{
//它将减去字母的ascii值
//从“a”开始,我们得到0到26之间的值)
hashArray[arr[i]-'a']=arr[i];
}

对于(i=0;如果当前元素将索引作为同一数组中元素的第一个索引,则保留该索引。如果不是,即索引不同,则表示有多个元素具有相同的值。将其删除。indexOf如何知道如何获取第一个找到的项的索引,它是如何创建的?@cres不确定您的要求是什么…
indexOf
的创建是为了返回第一个找到的元素的索引,是的。哦,基本上,不管有多少个a,indexOf(a)将指向第一个的索引,即0?@cres指向第一个找到的索引。@cres对象不可能有重复的键。每个新键都将覆盖下一个键。感谢后续操作。我现在得到+1。我可以对对象执行相同的过程吗?比如obj={a:1,a:3,b:2,c:5,d:6}有两个a属性,现在我想删除它。我认为筛选密钥没有任何意义,因为它们是自动消除重复的。在他的特定情况下,没有任何意义。但可能有一些情况下,您希望筛选出某些密钥。这应该是问题,对吗?为什么要发布答案一个随机的问题?
char arr[] = ['a','b','c','a','b','d','e','f'];
//it will contains all 26 places as zero (A - Z)
char hashArray[26]={0} 
int i; //for iteration;

for(i=0;arr[i]!='\0';i++)
{
  //it will subtracte the ascii value of the letter 
  //from 'a' so that we have the values from 0 to 26)
  hashArray[arr[i]-'a']=arr[i]; 
}

for(i=0;i<26;i++)
{
   if(hashArray[i]!=0) //to Ensure the positon has the character
   {
      printf("%c",hashArray[i]); 
   } 
}