Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.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 为什么forEach的索引在IntersectionObserver返回的数组中始终为0?_Javascript_Arrays_Intersection Observer - Fatal编程技术网

Javascript 为什么forEach的索引在IntersectionObserver返回的数组中始终为0?

Javascript 为什么forEach的索引在IntersectionObserver返回的数组中始终为0?,javascript,arrays,intersection-observer,Javascript,Arrays,Intersection Observer,我正在使用IntersectionObserver API 当输入特定部分时,背景色将更改。 为了处理这个问题,我必须获取IntersectionObserverEntry数组中输入的条目的索引,这里称为entries 我使用了forEach方法来获取条目的索引,但奇怪的是,它总是将索引设置为0。但当我通过获得的索引访问条目时,它工作得很好 const intersectionObserver = new IntersectionObserver(entries => { entrie

我正在使用IntersectionObserver API

当输入特定部分时,
背景色将更改。
为了处理这个问题,我必须获取
IntersectionObserverEntry
数组中输入的条目的索引,这里称为
entries

我使用了
forEach
方法来获取条目的索引,但奇怪的是,它总是将索引设置为
0
。但当我通过获得的索引访问条目时,它工作得很好

const intersectionObserver = new IntersectionObserver(entries => {
  entries.forEach((entry,index) => {
    if(entry.isIntersecting) {
      console.log('Intersecting',index);
      entry.target.className = entry.target.className.replace('hidden','fadeIn');
      changeBackgroundColor(entry.target.dataset.color);
      changeLogoColor(entry.target.dataset.theme);
    } else {
      console.log('Leave',index);
      entry.target.className = entry.target.className.replace('fadeIn','hidden');
    }
  });
});
const fadeInElemes = document.querySelectorAll('.hidden');
fadeInElemes.forEach((fadeInElem) => intersectionObserver.observe(fadeInElem));
结果如下

相交0
离开0
留下0

我的代码有什么问题?为什么索引总是0,但通过获取的索引进行访问会导致正确的元素

编辑


您可以访问forEach中
条目的索引,无需使用indexOf()


MDN web文档有很好的文档记录和很好的示例,在比较对象时,大多数情况下您都可以在那里找到您需要的内容,

IndexOf可能无法按预期工作:

constuser1={name:“nerd”,org:“dev”};
constuser2={name:“nerd”,org:“dev”};
常量eq=user1==user2;
console.log(eq);//给假
const myArr=[user1,user2];
console.log(myArr.indexOf(user1));//因为同一实例,所以给出0
console.log(myArr.indexOf(user2));//因为相同的实例,所以给出1
const myArr2=[{name:“nerd”,org:“dev”},{name:“nerd”,org:“dev”}];
console.log(myArr2.indexOf(user1));//给出-1,因为不是同一个实例

console.log(myArr2.indexOf(user2));//给出-1,因为不是同一个实例
您是从条目而不是从元素数组记录索引

fadeInElemes.forEach((fadeInElem) => intersectionObserver.observe(fadeInElem));

这是您获得实际查找的索引的地方。

您可以在observer中注册元素之前将索引链接到元素的属性重要!属性名称应唯一,以避免覆盖标准属性。

请阅读下面的评论,我已经将索引保存的代码行(18行)和读取的代码行(5行)分开

const observer=新的IntersectionObserver((条目,观察者)=>{
entries.forEach(entry=>{
//从元素的属性读取索引
让index=entry.target.elems\u index;
if(输入。isIntersecting){
log(索引,entry.target,'可见');
}否则{
log(索引,entry.target,'is hidden');
}
})
});
让elems=document.querySelectorAll('.elem');
forEach元素((元素,索引)=>{
//在observer中注册元素之前,将索引另存为元素的属性
elem.elems_index=索引;
观察者,观察者(elem);
});

打开pen的控制台查看结果:

im如果问题的关键不是
indexOf()
行为,请尝试记录我为此编辑的数组
条目。Ok indexOf失败,因为对象比较浅,只返回第一个项foundNope,尝试迭代并记录每个第二个参数(索引),您可能不需要indexOf(),因为IntersectionObserver回调中的数组项仅包含一项,而fadeInElemes正在设置观察者N次?所以每个相交的0,左0,左0都被我编辑的fadeInElems foreach observe callNope调用。它不起作用。请看我编辑的问题这真的是第一次进入
forEach
。但是更深一层,在
条目中,
i
每次都是0。即使在他们确实使用indexOf的地方,你的答案也没有任何意义
forEach((el,i,arr)=>arr.indexOf(el)==i)
将始终仅在没有重复条目的情况下生成
true
,这在回调IntersectionObserver@Kaiido
Array.indexOf()
返回找到的第一个元素的索引或-1。它不返回布尔值,mate。我引用console.log('Leave',entries.indexOf(entry)),总是返回0,因为它在console.log中总是在每次迭代mhaa时返回0。。。如果它返回0,因为它已在索引0处找到。因此,如果您的回答是因为两个对象文字不是相同的对象而找不到它们的对象,则完全是错误的。如果你是对的,那么他们会有-1。
fadeInElemes.forEach((fadeInElem) => intersectionObserver.observe(fadeInElem));