EN
JavaScript - example data for dirask (objects, classes, arrays etc.)
0
points
This article was created to store example data for JavaScript / Node.js such as objects, classes, arrays etc.
1. Class
class User {
constructor(id, name, age) {
this.id = id;
this.name = name;
this.age = age;
}
display() {
console.log(`id: ${this.id}, name: ${this.name}, age: ${this.age}`);
}
}
const user1 = new User(1, 'Tom', 23);
const user2 = new User(2, 'Kate', 25);
const user3 = new User(3, 'Ann', 22);
2. Array of objects (ENG)
Colors
const colors = [
{value: 'red', text: 'Red' },
{value: 'yellow', text: 'Yellow'},
{value: 'blue', text: 'Blue' }
];
Users
const users = [
{ id: 1, name: 'Kate', age: 25 },
{ id: 2, name: 'Tom', age: 23 },
{ id: 3, name: 'Ann', age: 26 },
{ id: 4, name: 'Jack', age: 21 }
];
Students
const students = [
{ id: 1, name: 'Kate', age: 25, favFruit: '🍏' },
{ id: 2, name: 'Tom', age: 23, favFruit: '🍌' },
{ id: 3, name: 'Ann', age: 26, favFruit: '🍊' },
{ id: 4, name: 'Jack', age: 21, favFruit: '🍒' }
];
2.1 Array of objects with date
var users = [
{ name: 'Ann', birthday: new Date('2000-01-02') },
{ name: 'Tom', birthday: new Date('2000-01-01') },
{ name: 'Mark', birthday: new Date('2000-01-03') }
];
with ISO 8601 date:
var array = [
{ name: 'Ann', birthday: '2000-01-02T00:00:00Z' },
{ name: 'Tom', birthday: '2000-01-01T00:00:00Z' },
{ name: 'Mark', birthday: '2000-01-03T00:00:00Z' }
];
3. Array of objects (PL)
Users
const users = [
{ id: 1, name: 'Kasia', age: 25 },
{ id: 2, name: 'Tomek', age: 23 },
{ id: 3, name: 'Ania', age: 26 },
{ id: 4, name: 'Jacek', age: 21 }
];
Students
const students = [
{ id: 1, name: 'Kasia', age: 25, favFruit: '🍏' },
{ id: 2, name: 'Tomek', age: 23, favFruit: '🍌' },
{ id: 3, name: 'Ania', age: 26, favFruit: '🍊' },
{ id: 4, name: 'Jacek', age: 21, favFruit: '🍒' }
];