0

How can I declare a JavaScript object which can hold infinite amount of values like given below? I am new to Javascript.

var activities = [
  {
    subject: "",
    genre :"",
    username: "",
    aboutuser: ""
  },
  {
    subject: "",
    genre :"",
    username: "",
    aboutuser: ""
  },
  {
    subject: "",
    genre :"",
    username: "",
    aboutuser: ""
  },
  ...
]
6
  • 2
    You don't have to predeclare the size of any variable in javascript; just declare an empty array and you can push onto it as many times as you like. Commented Dec 3, 2017 at 18:49
  • 2
    Probably arbitrary rather than infinite - your clients only have so much RAM. Commented Dec 3, 2017 at 18:50
  • Arrays are a dynamically sized type in JS. Do you actually need Array storage, or do you just need a type that will continuously generate them in a loop? What's the actual usage here? Commented Dec 3, 2017 at 18:52
  • You can declare an empty array: var arr = []; then use a for loop and push as many object as you want: for(var i = 0; i < 10; i++) { arr.push( { /* ... */ } ); }. (this adds 10 objects). Commented Dec 3, 2017 at 18:53
  • Are you looking for something like "infinite lists" using lazy evaluation? Commented Dec 3, 2017 at 18:55

1 Answer 1

2

Like this:

  let myArray = []

  myArray.push({
    subject: "",
    genre :"",
    username: "",
    aboutuser: ""
  })

Just define an empty array and push the object into it as many times you need.

Sign up to request clarification or add additional context in comments.

2 Comments

no problem. Just mark the answer as correct for other people future reference. Happy Coding
thats still definite ;D Some other solution for this?

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.