0

I am trying to push data into array in this code

let eyesData = [];

const pushFaceData = (
    { rightEyeOpenProbability },
    i
  ) => {
    //Value is getting printed  in console but not getting pushed
    //console.log(rightEyeOpenProbability);
    eyesData.push(rightEyeOpenProbability);
   
  };

const renderFaces = () => {
    faces.map(pushFaceData);
  };

console.log(eyesData);

faces is stream of data that updates itself in every 100ms so eyesData array should also be getting populated on each 100ms but in console, eyesData is always getting printed with empty value.

Array[]
Array[]
.
.
5
  • 3
    "faces is stream of data" What does that mean? It looks like an array, given you're calling map on it. (That should be forEach, btw; you're not mapping anything.) Commented Sep 29, 2021 at 12:29
  • 3
    Your code shows you creating the eyesData variable, assigning [] to it, and then logging it, with no intervening call to renderFaces or pushFaceData. That's why it's empty. I suspect there's more to it in your real code, but we can only work with what you show us. Please update your question with a minimal reproducible example demonstrating the problem, ideally a runnable one using Stack Snippets (the [<>] toolbar button); here's how to do one. Commented Sep 29, 2021 at 12:30
  • @T.J.Crowder agree with the map() thing, but loop should run right. And data should be pushed into eyesData. Right? Commented Sep 29, 2021 at 12:31
  • if(!eyesData){eyesData=[]}eyesData.push(rightEyeOpenProbability); Commented Sep 29, 2021 at 12:32
  • @TusharShahi - Not with the code above. It never calls renderFaces, so it never calls pushFaceData. But yes, if renderFaces were called, it would work (That's why I think there's more to it and hope the OP provides that minimal reproducible example, so we can help.). It's just misleading semantically and creates and throws away an unnecessary array. Commented Sep 29, 2021 at 12:48

1 Answer 1

1

If rightEyeOpenProbability is printing to the console, then there is no reason your code should be failing...

You are probably changing eyesData somewhere. For example, if you made a typo when checking its length, you could be erasing it:

if( eyesData.length = 0 ) { ... } //this line would erase eyesData[]'s contents
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.