const array = ['я в Симбирск,', 'в трактире.', '...']
const order = [3, 7, 0, 8, 11, 5, 9, 6, 4, 1, 12, 2, 10]
const strings = order.reduce((strings, number) => [...strings, array[number]], [])
const result = strings.join(' ')
function debounce(f, ms) {
let isRunning = false;
return function() {
if (isRunning) return;
f.apply(this, arguments);
isRunning = true;
setTimeout(() => isRunning = false, ms);
};
}
function myFunction {
console.log('i am working');
}
const debouncedMyFunction = debounce(myFunction, 2000);
debouncedMyFunction(); // Выполнится
debouncedMyFunction(); // Будет проигнорирована
this.array.splice(oldElementIndex, 1, newElement)
this.$set(this.array, oldElementIndex, newElement)
this.array = this.array.map(el => {
el.someParam = 1
return el
})
async getAllUsers() {
const { data: users } = await axios.get('http://jsonplaceholder.typicode.com/users')
this.posts = this.posts.map(p => {
p.author = users.find(u => u.id === p.userId)
return p
})
},
const filename = 'file.ext'
const ext = filename.split('.')[1] // 'ext'
const filename = 'file.ext'
const extSplitted = filename.split('.')
cosnt ext = extSplitted[extSplitted.length - 1]. // 'ext'
const filename = 'file.jpg'
const matched = filename.match(/jpg|mp3|png|mp4/)
if (matched !== null) console.log('file extension is:', matched[0])
const filename = 'file.jpg'
const matched = filename.replace(/\w+/, '').replace('.', '')
console.log(matched) // 'jpg'
const copyOfArray = JSON.parse(JSON.stringify(sourceArray))
const f = (arr = []) => {
arr.push(1)
arr.push(2)
return arr
}
const basicArr = f([]) // [1,2]
f(basicArr)
console.log(basicArr) // [1, 2, 1, 2]
data()
data: () => ({ param: 1 }) //Возвращает объект
data: () => {
return {
param: 1
}
}
this.inputs.forEach((value, index) => {
this.$set(this.inputs, index, {...value, label: value.label1})
});
this.inputs = this.inputs.map(item => ({ ...item, label: item.label1 }))
const obj = { a: 100, b: 152, c: { y: 133, x: { m: 20} } }
const flatObject = (current) => {
return Object.keys(current).reduce((acc, cur) => {
if (typeof(current[cur]) !== 'object') {
acc[cur] = current[cur]
} else {
acc = {...acc, ...flatObject(current[cur]) }
}
return acc
}, {})
}
console.log(flatObject(obj))