Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.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_Reactjs - Fatal编程技术网

Javascript 显示数组中的最大索引值

Javascript 显示数组中的最大索引值,javascript,reactjs,Javascript,Reactjs,我是一个新的反应者,我被困在我的一个练习中:我应该用特定的数组索引显示最大投票数。我怎么做 从“React”导入React,{useState} 从“react dom”导入react dom const App = (props) => { const [selected, setSelected] = useState(0) const [points, setPoints] = useState([0,0,0,0,0,0]); const showRandom= (

我是一个新的反应者,我被困在我的一个练习中:我应该用特定的数组索引显示最大投票数。我怎么做

从“React”导入React,{useState} 从“react dom”导入react dom

const App = (props) => {
  const [selected, setSelected] = useState(0)
  const [points, setPoints] = useState([0,0,0,0,0,0]);
 
 
 const showRandom= () => {
    const change = () => Math.floor(Math.random() * anecdotes.length);
    setSelected(change)
  }

  const setVote=()=>{
    const copy = { ...points };// copy the last state 
    copy[selected] += 1; //increase by 1 on the selected points
    setPoints(copy); // setting in setpoints the clicked values
    console.log('after update',points)
    
  }

  
 

  return (
    <div>
      {props.anecdotes[selected]}<br/>
      has {points[selected]} votes <br/>
      <button onClick={showRandom}>next anecdotes</button>
      <button onClick={setVote}>Vote</button>
      <h2>Anectodes with most vote:</h2><br/>
      
    </div>
  )
}


const anecdotes = [
  'If it hurts, do it more often',
  'Adding manpower to a late software project makes it later!',
  'The first 90 percent of the code accounts for the first 90 percent of the development time...The remaining 10 percent of the code accounts for the other 90 percent of the development time.',
  'Any fool can write code that a computer can understand. Good programmers write code that humans can understand.',
  'Premature optimization is the root of all evil.',
  'Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.'
]

ReactDOM.render(
  <App anecdotes={anecdotes} />,
  document.getElementById('root')
)
  
  
  
const-App=(道具)=>{
const[selected,setSelected]=useState(0)
常数[点,设定点]=使用状态([0,0,0,0,0]);
常量showRandom=()=>{
const change=()=>Math.floor(Math.random()*轶事.长度);
选举(变更)
}
const setVote=()=>{
const copy={…points};//复制最后一个状态
复制[所选]+=1;//在所选点上增加1
设定点(复制);//在设定点中设置单击的值
console.log('更新后',分)
}
返回(
{道具轶事[精选]}
拥有{点[选定]}票
下一篇轶事 投票 投票最多的轶事:
) } 常数轶事=[ “如果疼痛,就多做一次”, “为一个后期软件项目增加人力会使它变得更晚!”, '前90%的代码占前90%的开发时间…其余10%的代码占其他90%的开发时间。', “任何傻瓜都能写出计算机能理解的代码。好的程序员能写出人类能理解的代码。”, “过早优化是万恶之源。”, “调试的难度是一开始编写代码的两倍。因此,如果您尽可能巧妙地编写代码,从定义上讲,您不够聪明,无法调试它。” ] ReactDOM.render( , document.getElementById('root')) )
const copy={…points}这是错误的
它将是const copy=[…points]
原因点是一个数组
并且只使用轶事或道具。轶事,而不是两者

import React, { useState } from 'react';
const App = ({anecdotes}) => {
  const [selected, setSelected] = useState(0)
  const [points, setPoints] = useState([0,0,0,0,0,0]);
 const showRandom= () => {
    const change = () => Math.floor(Math.random() * anecdotes.length);
    setSelected(change)
  }
  const setVote=()=>{
    const copy = [...points];// copy the last state 
    copy[selected] += 1; //increase by 1 on the selected points
    setPoints(copy); // setting in setpoints the clicked values

  }

  const renderMostVote = () => {
    const maxVote = Math.max(...points); // get the maximum point

    // at first all one will have 0 vote
    if (maxVote === 0) return null;

    // in case multiple Anectodes have same number of max vote
    const maxList = [];
    points.forEach((el, index) => {
      if ( el === maxVote ) maxList.push(index);
    });

    // if just one Anectodes can get max vote then it will be:
    // points.findIndex(el => el === maxVote)

    return maxList.map((el, index) => (
      <p key={index} > {anecdotes[el]} -- with {points[el]} vote </p>
    ))

  }

  return (
    <div>
      {anecdotes[selected]}<br/>
      has {points[selected]} votes <br/>
      <button onClick={showRandom}>next anecdotes</button>
      <button onClick={setVote}>Vote</button>
      <div>
       <h2> Anectodes with most vote: </h2>
       {renderMostVote()}
      </div>
    </div>
  )
}
export default App;
import React,{useState}来自“React”;
常量App=({轶事})=>{
const[selected,setSelected]=useState(0)
常数[点,设定点]=使用状态([0,0,0,0,0]);
常量showRandom=()=>{
const change=()=>Math.floor(Math.random()*轶事.长度);
选举(变更)
}
const setVote=()=>{
const copy=[…points];//复制最后一个状态
复制[所选]+=1;//在所选点上增加1
设定点(复制);//在设定点中设置单击的值
}
常量renderMostVote=()=>{
const maxVote=Math.max(…点);//获取最大点
//首先,所有人都有0票
if(maxVote==0)返回null;
//如果多个轶事具有相同的最大投票数
常量maxList=[];
积分。forEach((el,索引)=>{
如果(el==maxVote)maxList.push(索引);
});
//如果只有一个轶事可以获得最大投票权,那么它将是:
//points.findIndex(el=>el==maxVote)
返回maxList.map((el,index)=>(

{轶事[el]}——用{points[el]}投票

)) } 返回( {轶事[精选]}
拥有{点[选定]}票
下一篇轶事 投票 投票最多的轶事: {renderMostVote()} ) } 导出默认应用程序;
const copy={…points}这是错误的
它将是const copy=[…points]
原因点是一个数组
并且只使用轶事或道具。轶事,而不是两者

import React, { useState } from 'react';
const App = ({anecdotes}) => {
  const [selected, setSelected] = useState(0)
  const [points, setPoints] = useState([0,0,0,0,0,0]);
 const showRandom= () => {
    const change = () => Math.floor(Math.random() * anecdotes.length);
    setSelected(change)
  }
  const setVote=()=>{
    const copy = [...points];// copy the last state 
    copy[selected] += 1; //increase by 1 on the selected points
    setPoints(copy); // setting in setpoints the clicked values

  }

  const renderMostVote = () => {
    const maxVote = Math.max(...points); // get the maximum point

    // at first all one will have 0 vote
    if (maxVote === 0) return null;

    // in case multiple Anectodes have same number of max vote
    const maxList = [];
    points.forEach((el, index) => {
      if ( el === maxVote ) maxList.push(index);
    });

    // if just one Anectodes can get max vote then it will be:
    // points.findIndex(el => el === maxVote)

    return maxList.map((el, index) => (
      <p key={index} > {anecdotes[el]} -- with {points[el]} vote </p>
    ))

  }

  return (
    <div>
      {anecdotes[selected]}<br/>
      has {points[selected]} votes <br/>
      <button onClick={showRandom}>next anecdotes</button>
      <button onClick={setVote}>Vote</button>
      <div>
       <h2> Anectodes with most vote: </h2>
       {renderMostVote()}
      </div>
    </div>
  )
}
export default App;
import React,{useState}来自“React”;
常量App=({轶事})=>{
const[selected,setSelected]=useState(0)
常数[点,设定点]=使用状态([0,0,0,0,0]);
常量showRandom=()=>{
const change=()=>Math.floor(Math.random()*轶事.长度);
选举(变更)
}
const setVote=()=>{
const copy=[…points];//复制最后一个状态
复制[所选]+=1;//在所选点上增加1
设定点(复制);//在设定点中设置单击的值
}
常量renderMostVote=()=>{
const maxVote=Math.max(…点);//获取最大点
//首先,所有人都有0票
if(maxVote==0)返回null;
//如果多个轶事具有相同的最大投票数
常量maxList=[];
积分。forEach((el,索引)=>{
如果(el==maxVote)maxList.push(索引);
});
//如果只有一个轶事可以获得最大投票权,那么它将是:
//points.findIndex(el=>el==maxVote)
返回maxList.map((el,index)=>(

{轶事[el]}——用{points[el]}投票

)) } 返回( {轶事[精选]}
拥有{点[选定]}票
下一篇轶事 投票 投票最多的轶事: {renderMostVote()} ) } 导出默认应用程序;
ok。。但是如何获得最大投票索引和值..你是指索引状态下的最大点数吗?我应该显示投票最多的轶事…我添加了rest行现在你可以获得投票最多的轶事谢谢你的回答,我尝试了以下方法,我无法正确显示函数maxVote(){const max=Math.max(…points)console.log('max value:',max)const index=points.indexOf(max);return(道具轶事[index])好的..但是如何获得最大投票指数和值..你的意思是有指数的状态的最大点数吗?我应该显示投票最多的轶事…我添加了rest行现在你可以获得投票最多的轶事谢谢你的回答,我尝试了以下方法,我无法正确显示函数maxVote(){const max=Math.max(…points)console.log('max value:',max)const index=points.indexOf(max);return(道具轶事[index])