0

Hi I have a variable containing two arrays :

var x={ arr1:[] , arr2:[]};

I want to push new element to arr1, I try this but the word "push" does not appear as an option:

(x.arr1).push("hi"); 

and

x.arr1.push("hi");

EDIT

I'm really sorry , that thing above actually works , I thought it is the same as if the variable x was global var defined in another js file,I am new to JS . So now let me explain more , x is a global variable defined in client.js file and there I set it with some properties, now I want to add more properties to it but from another js file called : server.js ,so I add these arrays to it as I mentioned above and when trying to push elements then it does not automatically completes the word "push".

client.js:

var gl={id: '123' , 
        myfunc: function(){console.log("client func");}
       };

server.js:

gl.arr1=[]; 
gl.arr1.push("hi");  //this does not work,it is like gl forgot about arr1 

Sorry again..

3
  • You don't show the whole picture in your question. The way it's written, the first array is accessed correctly (in both cases, though parentheses are redundant), so any correct autocomplete system should work. Commented Aug 24, 2017 at 21:44
  • You could try x.arr1.append["Hi"] Commented Aug 24, 2017 at 21:44
  • Then something else is wrong; works fine. {arr1: Array(2), arr2: Array(0)} arr1 : (2) ["hi", "hi"] arr2 : [] What do you mean by "doesn't appear as an option"? Object introspection only goes so far. Commented Aug 24, 2017 at 21:44

3 Answers 3

1

why not define it in the first place in client.js like this:

var gl={id: '123' , 
        myfunc: function(){console.log("client func");} , 
        arr1 : [] ,
        arr2 : []
       };

and in server.js use it as you mentioned above :

 gl.arr1.push("hi");
Sign up to request clarification or add additional context in comments.

1 Comment

because there might be more than tab/browser connected to the same server , in my case , chat application , for example if one user logged in after that some other users who already logged in and already have messages between each other , I want also for the new user to see the conversation from the beginning even before he logged
0

Try this:

x['arr1'].push('Hello')

Hope it Helps. Cheers!

1 Comment

No different than x.arr1 though.
0
var x = { arr1: [], arr2: [] };

x.arr1.push("hi");

console.log(x);

That's one way how it's done, what do you mean with "an option", you should try your snippets in codepen.io

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.