const shuffle = (arr) => {
for (let i = 0; i < arr.length - 1; i += 1) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
const test = () => {
let p = 0;
for (let i = 0; i < 100000; i += 1) {
const arr = Array(26).fill().map((el, idx) => idx % 2);
shuffle(arr);
p += arr[0] === arr[1] ? 1 : 0;
}
return p / 100000;
}
console.log(test()); // 0.47915
console.log(test()); // 0.48109
console.log(test()); // 0.47811
console.log(test()); // 0.47881
console.log(test()); // 0.48162
console.log(test()); // 0.48174 document.querySelector('#abc').innerText
// Uncaught TypeError: document.querySelector(...) is nullconst el = document.querySelector('#abc');
const text = el === null ? 'default Text' : el.innerText;const text = document.querySelector('#abc')?.innerText ?? 'default Text'; const removeDuplicates = (arr) =>
arr.reduce(
(acc, cur) => (acc.includes(cur) ? acc : (acc.push(cur), acc)),
[],
);
console.log(removeDuplicates([1,2,3,4,5,1,1,2,2,3,3,65,9,65,1,5,65, 117, 117, 118,118, 12333, 12333]));
// Array(10) [ 1, 2, 3, 4, 5, 65, 9, 117, 118, 12333 ] The maximum row size for an InnoDB table, which applies to data stored locally within a database page, is slightly less than half a page for 4KB, 8KB, 16KB, and 32KB innodb_page_size settings.
print json_encode(['a', 'b']); // ["a","b"]
print json_encode([0 => 'a', 1 => 'b']); // ["a","b"]
print json_encode([1 => 'a', 2 => 'b']); // {"1":"a","2":"b"}У вас массив заполняется по индексам $term->tid, которые не образуют классической последовательности, поэтому и представляются объектом.$vid = 'digest';
$termDataAll = array_map(
fn($term) => [
'tid' => $term->tid,
'name' => $term->name,
'weight' => $term->weight,
'childs' => array_map(
fn($child) => [
'tid' => $child->tid,
'name' => $child->name,
'weight' => $child->weight
],
\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid, $term->tid, 1)
)
],
\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid, 0, 1)
);[
[
"tid": "11",
"name": "parent-1",
"weight": "7",
"childs": [
["tid": "14", "name": "child-1", "weight": "0"],
["tid": "19", "name": "child-2", "weight": "1"],
],
], [
"tid": "12",
"name": "parent-2",
"weight": "6",
"childs": [
["tid": "17", "name": "child-1", "weight": "0"],
["tid": "18", "name": "child-2", "weight": "1"]
]
]
] float power(float X, int N) {
if (N == 0) {
return 1;
}
if (N < 0) {
return 1. / power(X, -N);
}
if (N % 2 == 0) {
float b = power(X, N / 2);
return b * b;
}
return X * power(X, N - 1);
}