Languages
[Edit]
EN

JavaScript - example data (objects, classes, arrays etc.)

0 points
Created by:
Elias999
399

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.1 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.2 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: '🍒' }
];

Dirask - content writing

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join