Languages
[Edit]
EN

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

0 points
Created by:
Elias999
819

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

 

Alternative titles

  1. Node.js - example data (objects, classes, arrays etc.)
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

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