Intl.ListFormat.prototype.format()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2021.
Die format()
-Methode von Intl.ListFormat
Instanzen gibt einen String mit einer sprachspezifischen Darstellung der Liste zurück.
Probieren Sie es aus
const vehicles = ["Motorcycle", "Bus", "Car"];
const formatter = new Intl.ListFormat("en", {
style: "long",
type: "conjunction",
});
console.log(formatter.format(vehicles));
// Expected output: "Motorcycle, Bus, and Car"
const formatter2 = new Intl.ListFormat("de", {
style: "short",
type: "disjunction",
});
console.log(formatter2.format(vehicles));
// Expected output: "Motorcycle, Bus oder Car"
const formatter3 = new Intl.ListFormat("en", { style: "narrow", type: "unit" });
console.log(formatter3.format(vehicles));
// Expected output: "Motorcycle Bus Car"
Syntax
format(list)
Parameter
list
-
Ein iterierbares Objekt, wie ein Array, das Strings enthält. Wenn dieses weggelassen wird, führt das zur Formatierung eines leeren Arrays, was etwas verwirrend sein könnte; es ist daher ratsam, immer explizit eine Liste zu übergeben.
Rückgabewert
Ein sprachspezifisch formatierter String, der die Elemente der Liste darstellt.
Hinweis:
Meistens ist die Formatierung, die format()
zurückgibt, konsistent. Allerdings kann die Ausgabe zwischen Implementierungen variieren, sogar innerhalb derselben Spracheinstellung — diese Variationen sind gewollt und durch die Spezifikation erlaubt. Es kann auch sein, dass die Ausgabe nicht der Erwartung entspricht. Zum Beispiel kann der String geschützte Leerzeichen verwenden oder von bidirektionalen Steuerzeichen umgeben sein. Sie sollten die Ergebnisse von format()
nicht mit fest codierten Konstanten vergleichen.
Beispiele
Verwendung von format
Das folgende Beispiel zeigt, wie ein Listenformatter mit der englischen Sprache erstellt wird.
const list = ["Motorcycle", "Bus", "Car"];
console.log(
new Intl.ListFormat("en-GB", { style: "long", type: "conjunction" }).format(
list,
),
);
// Motorcycle, Bus and Car
console.log(
new Intl.ListFormat("en-GB", { style: "short", type: "disjunction" }).format(
list,
),
);
// Motorcycle, Bus or Car
console.log(
new Intl.ListFormat("en-GB", { style: "narrow", type: "unit" }).format(list),
);
// Motorcycle Bus Car
Spezifikationen
Specification |
---|
ECMAScript® 2025 Internationalization API Specification # sec-Intl.ListFormat.prototype.format |
Browser-Kompatibilität
BCD tables only load in the browser