eval(JSON.stringify(array).replace(/[^\d]+/g, '+') + '0') // 31
const a = [1, 2, [3, 4, [[5, 6], 7], 8]];
// Вжух! и получится строка:
"+1+2+3+4+5+6+7+8+"
timeout
Type: Number
Set a timeout (in milliseconds) for the request. A value of 0 means there will be no timeout. This will override any global timeout set with $.ajaxSetup(). The timeout period starts at the point the $.ajax call is made; if several other requests are in progress and the browser has no connections available, it is possible for a request to time out before it can be sent. In jQuery 1.4.x and below, the XMLHttpRequest object will be in an invalid state if the request times out; accessing any object members may throw an exception. In Firefox 3.0+ only, script and JSONP requests cannot be cancelled by a timeout; the script will run even if it arrives after the timeout period.
str[i] = "x"
Так можно только читать.function reverse(str) {
var i, len = str.length, result = "";
for (i = len - 1; i >= 0; i--) result += str[i];
return result;
}
function getWithImages(result) {
return new Promise((res, rej) => {
const elPromises = [];
for (let key in result) {
if (!result.hasOwnProperty(key)) continue;
const element = result[key];
element.introtext = h2p(element.introtext);
element.fulltext = h2p(element.fulltext);
elPromises.push(
clientGI.search(element.title)
.then(images => {
element.images = images.map(image => [image.url, image.thumbnail]);
});
);
}
Promise.all(elPromises).then(function() {
// все готовы
res(result);
});
});
}
const gotImagesPromise = getWithImages(results[0]);
/**
* Разбивает строку на три части: цифры, две буквы, две буквы.
* Возвращает либо массив из трех элементов,
* либо undefined
*/
function getParts(str) {
const re = /^(\d+)(\S{2})(\S{2})$/;
const match = str.match(re);
if (match) return match.slice(1);
}
getParts("5дмсм") // ["5", "дм", "см"]
>> 1, 2, 3, 4, 5;
<– 5
undefined
.console.log()
. function camelCase(str) {
return str
.trim()
.split(' ')
.map((w, i, a) => {
if (i === 0) return w.toLowerCase();
return w.substr(0, 1).toUpperCase() + w.substr(1).toLowerCase();
})
.join('')
}
function test() {
const testIn = ['Good Day', 'good night', 'Good Evening', 'Sleep', 'Go'];
const testOut = ['goodDay', 'goodNight', 'goodEvening', 'sleep', 'Go'];
for (let i = 0; i < testIn.length; i++) {
const input = testIn[i];
const output = camelCase(input);
if (testOut[i] === output) console.log("Test passed: %s => %s", input, output);
else console.error("Test failed: %s => %s", input, output);
}
}
test();
function chooseBestSum(t, k, ls) {
/**
* make next combination of N on bits in a 32-bit integer
*/
function nextPerm(x) {
// via https://stackoverflow.com/questions/506807/creating-multiple-numbers-with-certain-number-of-bits-set
if (x === 0) return 0;
const smallest = (x & -x);
const ripple = x + smallest;
const new_smallest = (ripple & -ripple);
const ones = ((new_smallest/smallest) >> 1) - 1;
return ripple | ones;
}
let bestSum = null, bestN;
const len = ls.length;
if (len > 31) throw "Too many (over 31) options for this algorithm";
const maxmask = (1 << len) - 1;
if (len < k) return null; // not enough distances
ls.sort((a, b) => a - b); // todo: skip checking rest of combinations once solid selection of elements exceeds t
let mask = (1 << k) - 1; // initial mask value with k less significant bits on
let sum, pos, n;
while(true) {
for(sum = 0, n = 0, pos = 0; pos < 32; pos++) {
if (mask & (1 << pos)) {
sum += ls[pos];
if (++n === k) break;
}
}
if (sum > bestSum && sum <= t) {
bestSum = sum;
bestN = mask;
}
mask = nextPerm(mask);
if (mask > maxmask) break;
if (mask < 0) break;
}
return bestSum;
}
const data = [
[ '6', 'Shorter', '157' ],
[ '7', 'Fraser', '157' ],
[ '6', 'Chandter', '156' ]
// ...
];
const result = data.reduce((acc, cur) => {
acc.forEach((el, idx) => {el.push(cur[idx])});
return acc;
}, [[], [], []]);
// [["6","7","6"],["Shorter","Fraser","Chandter"],["157","157","156"]]
var numbers = [1,-3,5,-6,-10,13,4,-8];
var arr = numbers.slice(), sum = 0;
while((sum += arr.shift()) !== 0); // изысканный пустой while !
var index = numbers.length - arr.length - 1; // 5
function sum0(arr, idx = 0, sum = 0) {
if ((sum += numbers[idx]) === 0) return idx;
return sum0(arr, idx + 1, sum);
}
sum0([1,-3,5,-6,-10,13,4,-8]) // 5
toLowerCase()
map()
это много → многоreduce()
это много → одинvalues
получить массив объектов с полями родителя (это map()
).reduce()
).let result = segments
.map(e => e.value.map(
v => ({
value: v,
text : { hi: e.text.hi},
diff : e.diff
})
))
.reduce((prev, curr) => prev.concat(curr), [])
;