0

I have a function:

  chatManager.connect()

  .then(currentUser => {

    var final = currentUser.users;
    var name = [];
    var arr = [];

    $.each(final,function(index,val){

      if(val.name != '{{Auth::user()->name}}')
      {
        console.log(val.id);        //Harry, Ron (each in different line)

        arr = name.push(val.id)

        console.log(arr);   //1,2 (each in different line)

        var presence = val.presenceStore.store.{{Auth::user()->name}}{{Auth::user()->id}}

      }

    });

I want the arr to be an array like [Harry,Ron]. Why is it not working? I am new to Jquery. Please help.

2
  • what is the type of val.id? Does val.id returns Harry, Ron? Commented Jul 19, 2018 at 18:00
  • 1
    arr.push(val.id) (also not a jQuery issue at all) Commented Jul 19, 2018 at 18:01

1 Answer 1

4

arr = name.push(val.id) is your problem. push returns the array's new length, not an array. Simply replace the line with

arr.push(val.id);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, it works. But, when I use arr.push(val.id). I am also using push here. So, what is the difference?
@Rob The difference is explained in the first sentence of this answer. You are never actually pushing anything into arr.
Thnx for the help.

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.