let userList = '';
const myAjax = (url, method = 'GET', headers = {}) => {
return new Promise(resolve => {
$.ajax({
method: method,
url: url,
headers: headers,
dataType: 'json',
success: resolve,
});
})
}
(async () => {
let data = await myAjax("https://jsonplaceholder.typicode.com/todos/");
userList = data;
console.log(userList);
})()
var info = [{
desc: 'elem',
number: 10,
}, {
desc: 'elem2',
number: 20,
}, {
sum: 200,
}];
var sum = info.filter(i => i.sum).shift().sum;
var res = info.filter(i => i.number).map(m => (m.number / sum) * 100);
console.log(res);// [5, 10]
[1,"", null, NaN, 2, undefined,4,5,6].filter(Boolean); //[1, 2, 4, 5, 6]
[{
desc: 'elem',
number: 10,
}, {
desc: 'elem2',
number: 12,
}, {
count: 300,
}].filter(i => i.number).map( m => m.number); // [10, 12]
prompt
не работает. Количество пробелов:let myStr = prompt("text some...");
console.log(myStr.split(" ").length-1)
let myStr = prompt("text some...");
console.log(myStr.match(/\s/g).length)
[].find
подойдет, он возвращает первый найденный объект. Если id могут быть одинаковые и их несколько, то вам нужен [].filter
//find
[
{id:1, name: 'test'},
{id:2, name: 'test2'},
{id:1, name: 'test3'}
].find(item => item.id === 1) // {id:1, name: test}
//filter
[
{id:1, name: 'test'},
{id:2, name: 'test2'},
{id:1, name: 'test3'}
].filter(item => item.id === 1)
/*
вернет массив
[
{id:1, name: 'test'},
{id:1, name: 'test3'}
]
*/
var fullname = 'Вася';
var obj = {
fullname: 'Петя',
prop: {
fullname: 'Маша',
getFullname: function() {
return this.fullname;
}
}
};
var test = obj.prop.getFullname;
console.log(test.bind(obj.prop)());
console.log(test.apply(obj.prop));
console.log(test.call(obj.prop));
(function() {
console.log(obj.prop.getFullname());
})()
function close(e) {
document.getElementById('open').style.display = 'block';
document.getElementById('close').style.display = 'none';
}
function open(e) {
document.getElementById('close').style.display = 'block';
document.getElementById('open').style.display = 'none';
}
document.querySelector('#open').onclick = open;
document.querySelector('#close').onclick = close;
while (true) {
newUser = prompt('Введите имя и фамилию');
if (!newUser && newUser !== null) {
console.log('Вы не ввели данные!');
continue;
}
if (newUser != null) {
userName = newUser.replace(/\s+/g, ' ').trim().split(' ', 2);
if (userName.length < 2) {
console.log('Введите имя и фамилию');
continue;
}
reg = new User(userName[0], userName[1]);
console.log('есть данные', reg);
}
if (newUser === null) {
console.log('отмена');
break;
}
}
let food = [10];
let elem = {
calories: 555
};
const html = `<strong><a name="caloru" th:each="elem : ${food[0]}" th:text="${elem.calories}">тег а</a></strong>`;
document.body.insertAdjacentHTML('beforeend', html);
const el = document.querySelector('[name="caloru"');
const thEach = el.getAttribute('th:each');
const thText = el.getAttribute('th:text');
console.log(thEach, thText);