One way to achieve this task is by using JSOM and TypeScript. You first need to access the current context, then retrieve the site collection object, followed by obtaining the AllWebs collection which includes the Title property.
var context = new SP.ClientContext.get_current();
var allWebs = context.get_site().get_allWebs();
context.load(allWebs, 'Include(Title)');
var list = new Array();
clientContext.executeQueryAsync(succeeded, failed);
function succeeded(sender, args) {
for (var i = 0; i < allWebs.get_count(); i++) {
var subWeb = webSubSites.itemAt(i);
array.push(subWeb.get_title());
}
}
function failed(sender, args) {
console.log(args);
}
This code snippet is written in plain javascript. However, if you prefer using TypeScript, you can utilize the sharepoint.TypeScript.DefinitelyTyped package available on nuget.
To take advantage of TypeScript, you can define your callbacks (success, failed) as follows:
function succeeded(sender: any, args: SP.ClientRequestSucceededEventArgs) {
// implement success logic here
}
function failed(sender: any, args: SP.ClientRequestFailedEventArgs) {
// handle failures here
}