Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.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循环使用!变量_Javascript_Reactjs - Fatal编程技术网

Javascript forEach循环使用!变量

Javascript forEach循环使用!变量,javascript,reactjs,Javascript,Reactjs,我有这个功能。开始时,我有一个变量-canBeDeleted。它是一个数组;从我的老师那里得到了逗号,最好继续使用这个变量,而不是创建另一个变量(canNotBeDeleted),因为它们非常相似;所以我试图重写它,把它转换成布尔值(!canBedeleted)。但我有麻烦 最好的方法是什么 filterItems = () => { const { options, canBeDeleted } = this.props; const canNotBeDeleted = opti

我有这个功能。开始时,我有一个变量-canBeDeleted。它是一个数组;从我的老师那里得到了逗号,最好继续使用这个变量,而不是创建另一个变量(canNotBeDeleted),因为它们非常相似;所以我试图重写它,把它转换成布尔值(!canBedeleted)。但我有麻烦

最好的方法是什么

filterItems = () => {
  const { options, canBeDeleted } = this.props;
  const canNotBeDeleted = options.filter(item => !canBeDeleted.includes(item.id));
  options.forEach((gItem) => {
    const opt = gItem;
    canNotBeDeleted.forEach((item) => {
      opt.id === item.id && (opt.canNotBeDeleted = true);
    });
  });
};

您可以省略
选项.forEach
,只使用
不能删除的.forEach
,如下所示。由于您已经在筛选您的
选项
canNotBeDeleted
是需要设置
canNotBeDeleted=true的数组

filterItems = () => {
  const { options, canBeDeleted } = this.props;
  const canNotBeDeleted = options.filter(item => !canBeDeleted.includes(item.id));
  canNotBeDeleted.forEach((gItem) => {
    gItem.canNotBeDeleted = true;
  });
};

您可以省略
选项.forEach
,只使用
不能删除的.forEach
,如下所示。由于您已经在筛选您的
选项
canNotBeDeleted
是需要设置
canNotBeDeleted=true的数组

filterItems = () => {
  const { options, canBeDeleted } = this.props;
  const canNotBeDeleted = options.filter(item => !canBeDeleted.includes(item.id));
  canNotBeDeleted.forEach((gItem) => {
    gItem.canNotBeDeleted = true;
  });
};

只需使用一个for循环

像这样

filterItems=()=>{
const{options,canBeDeleted}=this.props;
for(设i=0;i
只需使用一个for循环即可

像这样

filterItems=()=>{
const{options,canBeDeleted}=this.props;
for(设i=0;i
hi-它工作正常吗?如果有任何问题,请告诉我。嗨,工作正常吗?有任何问题请告诉我。