Junior — Middle
What methods do you usually use to implement data grouping storage in JavaScript?
sobes.tech AI
Answer from AI
In JavaScript, the following data structures are commonly used:
- Arrays (
Array) — ordered collections of elements, suitable for lists where order and index access are important.
const fruits = ['apple', 'banana', 'cherry'];
- Objects (
Object) — key-value pairs, convenient for storing data with named properties.
const user = { name: 'Ivan', age: 30 };
- Map — a collection of key-value pairs where keys can be of any type (not just strings), maintaining insertion order.
const map = new Map();
map.set('key', 'value');
map.set(1, 'number');
- Set — a collection of unique values, useful for storing a set without duplicates.
const set = new Set([1, 2, 3, 2]); // contains 1, 2, 3
The choice depends on the task: use an array for ordered lists, Map for associative arrays with different key types, Set for unique elements, and object for simple key-value pairs.