It seems you need to give is an array with id and text
https://select2.org/data-sources/formats
You are just passing the values directly in an array
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am trying ti display all citys of ex:London from an json file downloaded from here: (https://simplemaps.com/data/gb-cities) to my select option . But something is going wrong, the select show me no data. But in console log i can see all city in return
var data = fetch('../json/gb.json')
.then(response => response.json())
.then(json => json.map(x => x.city));
console.log(data);
var placeholder = "select";
$(".mySelect").select2({
data: data,
placeholder: placeholder,
allowClear: false,
minimumResultsForSearch: 5
});
console.log(data) return me this
Promise {<pending>}
[[Prototype]]
:
Promise
[[PromiseState]]
:
"fulfilled"
[[PromiseResult]]
:
Array(2680)
0 : "London"
1:"Birmingham"
2: "Manchester"
3: "Leeds"
4: "Newcastle"
.....
//My select neturn no data
You need to wait for the response to finish
let data = fetch('../json/gb.json')
.then(response => response.json())
.then(json => {
let cities = json.map(x => {"id": x.city, "text": x.city});
let placeholder = "select";
$(".mySelect").select2({
data: cities,
placeholder: placeholder,
allowClear: false,
minimumResultsForSearch: 5
});
});
console.log(data);
Please or to participate in this conversation.