0

This may seem like an easy question, but im stuck. Currently, I am passing an object over to my function, which in return is supposed to push an object's value to an array. As of now its not pushing INTO the array and it coming back as a count 0 and undefined if I try to get at the actual value explicitly.

In my check file, i have a function call with a forloop that sends the object and the count

for (let i=0; i< data.length; i++){
            addtoList(i, data);  
        }
     });

the forloop is looping through data returned from a Django object as such

data = serialize("json", modelname.filter(username=...))
return HttpResponse(data)

In my addto file, the actual function should handle adding the object to the list.

var pushere = []

function addToList(i,data){
pushere.push(data[i].fields.title)
console.log(data[i].fields.title)
};

console.log(pushere)

now i also logged a regular list to see the differences

const testListt = ['a','b','v']
console.log(testListt)

with that being said, how am i supposed to properly 'push' object values to an array, which is what i am needing before displaying it to the page. I've tried mapping objects with help from Mozilla Map and their help on Objects.

When a user first logs in, it seems to work, but upon a page refresh i am not able to get at the value. *first login Heres whats logged when the user is first logged in.

*on refresh After user logs in and the page is refreshed

Would saving it into the localstorage and getting the value from there work instead?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
gdub97
  • 1
  • 1
  • 1
    "*As of now its not pushing INTO the array and it coming back as a count 0 and undefined if I try to get at the actual value explicitly.*" is the function asynchronous? See [console.log() shows the changed value of a variable before the value actually changes](https://stackoverflow.com/q/11284663) for the second screenshot - it does show that the array was empty when it was logged and then later filled before you expended it in the console. – VLAZ Nov 10 '22 at 15:13

0 Answers0