I need to make multiple objects of same element. I cant figure it out that which should I use between [] and {} to make my data structure.
I am not clear about the difference between [] and {}. I am using Angularjs
I need to make multiple objects of same element. I cant figure it out that which should I use between [] and {} to make my data structure.
I am not clear about the difference between [] and {}. I am using Angularjs
Share Improve this question edited Mar 16, 2016 at 8:27 Zibellino 1,4336 gold badges26 silver badges33 bronze badges asked Mar 16, 2016 at 8:18 n3018n3018 1291 gold badge2 silver badges12 bronze badges 4- What language are you using? – TheBigH Commented Mar 16, 2016 at 8:19
- could you just precise which languare you are using ? – xiawi Commented Mar 16, 2016 at 8:19
- 1 AngularJS is a JavaScript framework (read the tag) so chances are it's JavaScript indeed. – Zibellino Commented Mar 16, 2016 at 8:23
- I need to know what is the difference between using [] and {} to pass elements in the angularjs javascript controller – n3018 Commented Mar 16, 2016 at 8:25
2 Answers
Reset to default 5[]
represents Array
{}
represents Object
The advantage of using array is the ability to use native functions like forEach
, sort
, split
, splice
and more.
The advantage of using object is to getting easy way to find specific item from list of items.
For example, if you have a list of objects with unique ID
for each one
and you create object that contain them by ID
:
var list = {"123": {ID: 123, ...}, "456": {ID: 456, ...}}
Now you can find item by ID
without searching in all items for this ID
just with list[ID]
Use []
if you want an array a.k.a. a list of objects. Use {}
if you want to create a single object.