const result = Array(arr.length);
for (let i = 0; i < arr.length; i++) {
result[i] = arr[i][0] || '';
}
// или
const result = [];
for (const [ n = '' ] of arr) {
result.push(n);
}
// или
const result = [];
while (result.length < arr.length) {
result[result.length] = arr[result.length].slice(0, 1);
}
// или
const result = arr.map(n => n.charAt(0));
const towns = [
'Бердичев',
'Великая Чернетчина',
'Геническ',
'Гнездычев',
'Черкассы'
];
const query = 'че';
let result = towns
.map(town => ({
name: town,
index: town.toLowerCase().indexOf(query),
}))
.filter(town => town.index >= -1)
.sort((a, b) => a.index - b.index)
.map(town => town.name);
$.ajaxSetup({
error: function (x, status, error) {
console.log("An error occurred: " + status + " nError: " + error + " " + x);
}
});
$.ajax({
url: "/some/route",
dataType: "json",
type: "POST",
data: {"key": key, "mode": mode},
success: (function (data) {
if (data == "ok") {
alert('its ok!');
doSomeCallBack(data)
}
})
})
function but(id) {
fetch('/ajax', {method: 'POST',body:JSON.stringify({
name:'vlad',
id:id
}),headers:{'content-type': 'application/json'}})
.then(function (response) {
alert(response.status); // 200
return response.json();
})
.then(function (data) {
alert(data.id);
let elem = document.getElementById('h2o');
elem.innerText = data.id;
})
.catch(alert);
}
SyntaxError: Unexpected token < in JSON at position 0
fetch(`http://api.openweathermap.org/data/2.5/forecast/?q=${this.state.value}&cnt=1&APPID=e94f24c463ec7a0d1a69ebdebc88c694&lang=ru`)
.then(res => res.json())
.then(json => console.log(json));