R
R
rus762021-12-19 13:53:56
JavaScript
rus76, 2021-12-19 13:53:56

How to fill a JS array with data without keys?

The question is terribly simple, but after reading a bunch of links in Yandex, I realized that everyone writes the same thing, declaring an array in the examples array = ["a", "b"]. Cool, but who statically populates the array with data? I don’t understand why these examples at all (

Task: Get an array of the form ['a', 'b', 'c']

What I do:
1. I declare an array
payments_name = []
2. In the each loop, I add values ​​using the push method
payments_name.push(value);
3. As a result, I get an array with keys like [0 => 'a', 1=>'b']

Please help (

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
true, 2021-12-19
@RAFAILgaley

an array is just a sequence of values
​​and an object is a set of values
​​an array is the same object
but in an object the keys are unique strings
and in an array it's a serial number

K
Kirill Gusarev, 2021-12-19
@kaka888

It would be pointless to add something to the array, if you can’t access this element later in any way (that is, by index (key)).
The index (key) of an array element is just its position in the array. You always put a new element on some certain position (index/key).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question