➕︎ Aa ➖︎ |Dark 1 ● Dark 2 ● Light|Hack ● Fira Code|Back to PT Mono✖
// CPCS-324 Algorithms and Data Structures 2
// Language demo - Array of objects
// 2020, Dr. Muhammad Al-Hashimi
// note output string inside a paragraph <p></p>
document.write( "<p>Array of Objects:</p>" );
// ---------------------------------------------------------
// define object literal (or constant) inside { }
// vertex is array of 2 objects (formatted to clarify syntax)
var vertex = [
{label: "a", adjacent: null},
{label: "b", adjacent: null}
];
// a vertex object has 2 property fields (also called member
// variables): label, adjacent
// we can add other fields such as "visit" to mark visited
// vertices, or "color" to classify vertices
// ---------------------------------------------------------
// reference a vertex using usual array notation, example:
// vertex[0], vertex[1] etc.
document.write("<p>2nd vertex: ", vertex[1], "</p>");
// reference property field using usual "." notation,
// example: vertex[0].label
document.write("<p>Label of first vertex is: ", vertex[0].label, "</p>");
// note document.write() can't show user object details
// you write your own code to show vertex fields
document.write("<p>{ Label: ", vertex[1].label, ", Adjacent: ", vertex[1].adjacent, " }</p>");
// note array (assigned to vertex), a standard
// format called JSON: Javascript Object Notation
// more on this later (see json.org)