Home > Back-end >  I want to pass this value in to array
I want to pass this value in to array

Time:10-06

I want to add useeffect jobTitle and the noOfvacancy to the mydata array. my data array is dynamic and its size is automaticaly change using useeffect for loop.

Finaly my target is pass data to display a pie chart

Image is attached below,

enter image description here

useEffect(() => {
    for (let i = 0; i < vacancy.length; i  ) {
      var jobTitle = vacancy[i].jobTitle;
      var noOfVacancy = vacancy[i].noOfVacancy;
      console.log(jobTitle, noOfVacancy);
    }
  }, [vacancy]);

  const myData = [
    
    { x: "PHP", y: 90 },
    { x: "Python", y: 400 },
    { x: "Javascript", y: 300 },
  ];

CodePudding user response:

Better to use useState like so

const [myData, setMyData] = useState([  
    { x: "PHP", y: 90 },
    { x: "Python", y: 400 },
    { x: "Javascript", y: 300 },
  ]);

useEffect(() => {
    const data = myData.slice();
    for (let i = 0; i < vacancy.length; i  ) {
      var jobTitle = vacancy[i].jobTitle;
      var noOfVacancy = vacancy[i].noOfVacancy;
      data.push({x: jobTitle, y: noOfVacancy});
    }
    setMyData(data)
  }, [vacancy]);

Don't forget to import useState

  • Related