I am working in JS, for what I am doing I use an array or object, the truth is that I am not sure that I can store several records to give you an example:
arr[0].name = "Angel"
arr[0].age = 19
arr[0].kg = 70
arr[1].name = "Louis"
arr[1].age = 22
arr[1].kg = 75
Since I need to put all that information in a single variable to be able to build a table that I want, but I can't find a way in JS to do it.
You can put several objects in an array. The key to everything in Javascript is use
[]
for array and use{}
for object.For example here:
allPeople
is an array, and inside it has several objects:{}
.Let's see a test and its route:
Suppose you want to modify the array once it's created, since you can add other objects to it using
push
.For example:
For more details see the documentation:
An array is basically a list, a set of elements whose items are separated by a comma (it can contain objects and it is not necessary for all its elements to be of the same type), to access its elements it is done by position, starting with 0 , then accessing the first element in the following way nameArray[0] , if you want to access another element of the array, you only have to indicate the corresponding index.
An object has a structure of key/value pairs, each pair is separated by a comma. To access the value that corresponds to a property, one way to achieve it is as follows : ObjectName.PropertyName (eg person.name )
Array example:
Object example:
So you could do something like this:
And to access each one you could do: console.log(people[0].name) //It would return "Angel" console.log(people[1].name) //It would return "Pedro" console.log(people[ 2].kg) //Would return 62
It would also be possible to use a loop to traverse the array and do something for each element, eg: