let [0: [fruit1, fruit2, fruit3]] = arr; //Uncaught SyntaxError: Invalid destructuring assignment target
const arr = [
[ 1, 2, 3 ],
[ 5, 6, 7 ],
[ 7, 8, 9 ],
];
const [ , [ , val1 ], [ ,, val2 ] ] = arr;
console.log(val1, val2); // 6 9
const arr = [
[ 1, 2, 3 ],
[ 5, 6, 7 ],
[ 7, 8, 9 ],
];
const { 1: { 1: val1 }, 2: { 2: val2 } } = arr;
console.log(val1, val2); // 6 9
props: {
modelValue: {
type: String,
default: '',
},
},
emits: [ 'update:modelValue' ],
setup(props, { emit }) {
const value = ref('');
watchEffect(() => value.value = props.modelValue);
return {
value,
onInput: e => emit('update:modelValue', value.value = e.target.value),
};
},
<input :value="value" @input="onInput">
computed: {
cartTotalCost() {
return this.cart_data.reduce((acc, n) => acc + n.price * n.amount, 0);
},
...
Object.values(t).forEach(n => {
n.children?.sort((a, b) => (a.order - b.order) || a.name.localeCompare(b.name));
});
const square = n => n ** 2;
// или
const square = n => n * n;
// или
const square = n => Math.pow(n, 2);
arr.forEach((n, i, a) => a[i] = square(n));
// или
arr.splice(0, arr.length, ...arr.map(square));
// или
for (const [ i, n ] of arr.entries()) {
arr[i] = square(n);
}
// или
for (let i = 0; i < arr.length; i++) {
arr[i] = square(arr[i]);
}
Как мне изменить код, чтобы исправить ошибку?
const countries = Array.from({ length: 3 }, prompt);
new gridjs.Grid({
columns: [
'Code',
{ name: 'Flag', formatter: val => gridjs.html(`<img src="${val}">`) },
'Name',
'Capital',
'Population',
],
data: () => Promise.all(countries.map(n =>
fetch(`//restcountries.com/v3.1/name/${n}`)
.then(r => r.json())
.then(([ r ]) => [
r.altSpellings[0],
r.flags.png,
r.name.common,
r.capital[0],
r.population,
])
.catch(() => [ ,,`${n} - это не страна`,, ])
)),
}).render(document.querySelector('#wrapper'));
(str.match(/[0-9]+/g) ?? []).map(Number)
// или
Array.from(str.matchAll(/\d+/g), n => +n)
// или
str.split(/\D+/).filter(Boolean).map(parseFloat)
// или
eval(`[${str.replace(/\D+/g, (m, i) => i ? ',' : '')}]`)
// или
[...str].reduce((acc, n, i, a) => (
isNaN(n) || (isNaN(a[i - 1]) && acc.push(0), acc.push(acc.pop() * 10 + n * 1)),
acc
), [])
isValid() {
return Object.values(obj).flatMap(Object.values).every(n => n.valid);
},
objects() {
return Object.values(this.obj).flatMap(Object.values);
},
isValid() {
return this.objects.every(n => n.valid);
},
ищет метод toString - его нет
test.toString
, то результатом будет undefined
. Вы проверьте, так ли это. Будете удивлены. const tables = document.querySelectorAll('селектор таблиц');
for (const { tHead, tBodies } of tables) {
const labels = Array.prototype.map.call(
tHead.rows[0].cells,
th => th.textContent
);
for (const { rows } of tBodies) {
for (const { cells } of rows) {
for (const [ i, label ] of labels.entries()) {
cells[i].dataset.label = label;
}
}
}
}
tables.forEach(table => {
table.querySelectorAll('tbody td').forEach(function(td) {
td.setAttribute('data-label', this[td.cellIndex]);
}, Array.from(table.querySelectorAll('thead th'), th => th.innerText));
});
Задача проверить является ли список палиндромом, я делаю самое банальное, дан список head, я проверяю return head == head[: :-1]
<router-link :to="`/Product/${item}`">{{ item }}</router-link>
const result = data
.flatMap(n => n.values)
.filter(n => n.Ids.some(m => arr.includes(m)))
.map(({ id, title }) => ({ id, title }));
const inArr = Set.prototype.has.bind(new Set(arr));
const data.reduce((acc, { values }) => (
values.forEach(({ Ids, ...n }) => Ids.some(inArr) && acc.push(n)),
acc
), []);
const result = allCass.filter(function(n) {
return !this.has(n.id);
}, new Set(defaultCass.map(n => n.id)));
function* diff(data1, data2, key = n => n) {
const getKey = key instanceof Function ? key : n => n[key];
const keys = new Set;
for (const n of data2) {
keys.add(getKey(n));
}
for (const n of data1) {
if (!keys.has(getKey(n))) {
yield n;
}
}
}
const result = [...diff(allCass, defaultCass, 'id')];
Array.from(diff('abcdE', 'AcD', n => n.toLowerCase())) // ['b', 'E']
for (const n of diff(Array(8).keys(), Array(5).keys())) {
console.log(n); // 5 6 7
}
const Todos = ({ todos, TodoItem }) => (
<div className="todos">
{todos.map(n => (
<div className="todo-item" key={n.id}>
<TodoItem todo={n} />
</div>
))}
</div>
);
const HeaderTodoItem = ({ todo }) => (
<h3>{todo.title}</h3>
);
const AppTodoItem = ({ todo }) => (
<>
<img src={todo.img} alt={todo.title} />
<div>
<h3>{todo.title}</h3>
<p>{todo.text}</p>
</div>
</>
);
<Todos todos={todos} TodoItem={HeaderTodoItem} />
<Todos todos={todos} TodoItem={AppTodoItem} />
Пытался сделать через reduce из библиотеки lodash
const newArr = _.map(_.groupBy(arr, 'merch'), (v, k) => ({
merch: +k,
games: _.uniq(_.flatMap(v, 'games')),
}));
const newArr = Object
.entries(arr.reduce((acc, n) => ((acc[n.merch] ??= []).push(...n.games), acc), {}))
.map(n => ({ merch: +n[0], games: [...new Set(n[1])] }));