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 sprachspezifischen String mit der 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 Zeichenketten enthält. Wenn es weggelassen wird, wird das leere Array formatiert, was leicht verwirrend sein kann. Daher ist es ratsam, immer explizit eine Liste zu übergeben.
Rückgabewert
Ein sprachspezifisch formatierter String, der die Elemente der Liste darstellt.
Hinweis:
Meistens ist das durch format()
zurückgegebene Format konsistent. Allerdings kann die Ausgabe zwischen verschiedenen Implementierungen variieren, selbst innerhalb derselben Spracheinstellung – diese Variationen sind im Design vorgesehen und durch die Spezifikation erlaubt. Es ist möglicherweise auch nicht das, was Sie erwarten. Zum Beispiel könnte 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 Listen-Formatter 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® 2026 Internationalization API Specification # sec-Intl.ListFormat.prototype.format |