[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat [Back]  [Original]

Intl.ListFormat - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Intl.ListFormat

20214

Intl.ListFormat

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"

new Intl.ListFormat([locales[, options]])

locales

. BCP 47 locales Intl page.

options

  • localeMatcher "lookup" "best fit""best fit"Intl page.
  • type "conjunction" ( A, B, and C) "disjunction"( A, B, or C)"unit" ( 5 pounds, 12 ounces).
  • style "long" (A, B, and C)"short" "narrow" (A, B, C)style narrow type unit

Intl.ListFormat.prototype

Intl.ListFormat.supportedLocalesOf()

format

js
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

formatToParts

js
const list = ["Motorcycle", "Bus", "Car"];
console.log(
  new Intl.ListFormat("en-GB", {
    style: "long",
    type: "conjunction",
  }).formatToParts(list),
);

// > [ { "type": "element", "value": "Motorcycle" }, { "type": "literal", "value": ", " }, { "type": "element", "value": "Bus" }, { "type": "literal", "value": ", and " }, { "type": "element", "value": "Car" } ];

ECMAScript 2027 Internationalization API Specification
# listformat-objects


Web Proxy Viewer  |  New URL  |  Original Page