【问题标题】:Why is array.findIndex returning not a fucntion. (mozilla says it basically is?)为什么 array.findIndex 返回不是函数。 (Mozilla说它基本上是?)
【发布时间】:2021-11-13 12:52:21
【问题描述】:

将state.findIndex 更改为state.ids.findIndex 解决了这个问题。

但是,当调用函数onClick 时,它会自动将所有active 值更新为true。而不是等待点击事件。


我正在尝试设置一个onClick 方法/函数,它将找到active: true 并将其设置为false,然后找到具有匹配id: xxxxxx 的对象并将该对象的active: 设置为@ 987654334@.

根据 developer.mozilla.org 和其他几个网站,我的方法理论上应该有效,或者,我更可能误解了它。

这是它返回的错误

这是我正在尝试编写的函数 - 为什么它返回 findindex 不是函数?

function changeActiveField(im) {
  const i = state.findIndex((obj) => obj.active === true);
  state[i].active = false;
  const index = state.findIndex((obj) => obj.id === im);
  state[index].active = true;
}

const i 在这里找到obj{ active: true} 的索引并将其更改为obj{ active: false} 然后const index 找到与obj{id: value} 匹配的obj,该im 是从单击的组件传入的...

<div className="thumbs">
          {state.ids.map((i) => (
              <Image
                className="carouselitem"
                rounded
                fluid
                onClick={changeActiveField(i.id)}
                src={"http://img.youtube.com/vi/" + i.id + "/hqdefault.jpg"}
                size="small"
              />
          ))}
        </div>

转到https://test.ghostrez.net 并导航到“服务”页面以查看视频菜单。小缩略图是应该触发changeActiveField函数的“可点击”项目,依次将点击的id设置为active: true并更改activevid div

这是完整的页面代码。

import React from "react";
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
import { Embed, Image, Loader } from "semantic-ui-react";
import "./Player.css";

var state = {
  ids: [
    {
      id: "iCBvfW08jlo",
      active: true,
    },
    {
      id: "qvOcCQXZVg0",
      active: false,
    },
    {
      id: "YXNC3GKmjgk",
      active: false,
    },
  ],
};

function firstActiveId(ids) {
  for (var i = 0; i < ids.length; i++) {
    if (ids[i].active) {
      return ids[i].id;
    }
  }
}

function changeActiveField(im) {
  const i = state.findIndex((obj) => obj.active === true);
  state[i].active = false;
  const index = state.findIndex((obj) => obj.id === im);
  state[index].active = true;
}

export default class Player extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      state: false,
    };
  }
  render() {
    return (
      <div className="carouselwrap">
        <div className="activevid">
          <Embed
            active
            autoplay={false}
            color="white"
            hd={false}
            id={firstActiveId(state.ids)}
            iframe={{
              allowFullScreen: true,
              style: {
                padding: 10,
              },
            }}
            placeholder={
              "http://img.youtube.com/vi/" +
              firstActiveId(state.ids) +
              "/hqdefault.jpg"
            }
            source="youtube"
          />
        </div>
        <div className="thumbs">
          {state.ids.map((i) => (
            <>
              <Image
                className="carouselitem"
                rounded
                fluid
                onClick={changeActiveField(i.id)}
                src={"http://img.youtube.com/vi/" + i.id + "/hqdefault.jpg"}
                size="small"
              />
              <h2>
{/*this is for testing purposes only*/}
                {i.id} {i.active ? "true" : "false"}
              </h2>
            </>
          ))}
        </div>
      </div>
    );
  }
}

【问题讨论】:

  • .findIndex() 是一个 array 方法;在我看来 state 不是一个数组。
  • state 中的 id 属性是一个数组。使用state.id.(...)
  • 尝试 state.ids.findIndex(....) 您的状态是一个具有一个 ids 属性的对象。 ids 的值就是你要定位的数组。
  • 谢谢大家。解决了这个问题。由于某种原因,它现在将所有对象的活动值标记为真。不等待按钮点击。 ://

标签: javascript reactjs


【解决方案1】:

问题是状态不是数组,而是数组中的 ids...所以将我的语句更改为 state.ids.findIndex 解决了这个问题

function changeActiveField(im) {
  const i = state.findIndex((obj) => obj.active === true);
  state[i].active = false;
  const index = state.findIndex((obj) => obj.id === im);
  state[index].active = true;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多