基本上我想要一个最大长度为 4 的数组类型。很容易我无法找到如何在打字稿中实现此检查。有人可以帮我吗?像这样的东西:
const a = [item1, item2, item3, item4, *item5*] -> array has a maximum length of 4
Run Code Online (Sandbox Code Playgroud)
谢谢!
我最近偶然发现了这个函数,它可以确定某个东西是否是普通对象(JavaScript):
function isPlainObject (value){
if (typeof value !== 'object' || value === null) return false;
let proto = value;
while (Object.getPrototypeOf(proto) !== null) {
proto = Object.getPrototypeOf(proto);
}
return Object.getPrototypeOf(value) === proto;
};
Run Code Online (Sandbox Code Playgroud)
来源: https: //github.com/redux-utilities/redux-actions/blob/master/src/utils/isPlainObject.js
我想知道:
function isPlainObj(value){
if (typeof value !== 'object' || value === null) return false;
let obj = {};
return Object.getPrototypeOf(value) === Object.getPrototypeOf(obj)
}
Run Code Online (Sandbox Code Playgroud) I have the following html code:
<nav>
<ul>
<li>Text1</li>
<li>Text2</li>
<li>Text3</li>
</ul>
</nav>
Run Code Online (Sandbox Code Playgroud)
Using:
nav :last-child {
text-transform: uppercase;
}
Run Code Online (Sandbox Code Playgroud)
Result is:
Using:
nav li:last-child {
text-transform: uppercase;
}
Run Code Online (Sandbox Code Playgroud)
Result is:
While using:
nav :not(:last-child) {
text-transform: uppercase;
}
Run Code Online (Sandbox Code Playgroud)
Result is:
Why ":not(:last-child)" doesn't need "li" to target the last child? Thanks.
class App {
constructor() {
this.canvas = document.createElement('canvas');
document.body.appendChild(this.canvas);
this.ctx = this.canvas.getContext('2d');
this.pixelRatio = window.devicePixelRatio > 1 ? 2 : 1;
window.addEventListener('resize', this.resize.bind(this), false);
this.resize();
window.requestAnimationFrame(this.animate);
}
resize() {
this.stageWidth = document.body.clientWidth;
this.stageHeight = document.body.clientHeight;
}
animate = () => {
this.test(); // ---> here!
};
test = () => {
console.log('here!');
};
}
window.onload = () => {
new App();
};Run Code Online (Sandbox Code Playgroud)
箭头函数不会被提升,只有常规函数会被提升。为什么animate函数内部可以调用this.test?类中箭头函数的不同行为?
无法new Map在 Typescript 中传递二维数组:
const myArray: string[][] = [['DE', 'Germany'], ['AU', 'Austria']];
const myMap = new Map(myArray);
Run Code Online (Sandbox Code Playgroud)
对我来说失败了我TS2769: No overload matches this call.
在这里做错了吗?
TS 版本 3.9.9
我是 splunk 的新手。我有这个 json:
"request": {
"headers": [
{
"name": "x-real-ip",
"value": "10.31.68.186"
},
{
"name": "x-forwarded-for",
"value": "10.31.68.186"
},
{
"name": "x-nginx-proxy",
"value": "true"
}
Run Code Online (Sandbox Code Playgroud)
当属性名称具有“x-real-ip”值时,我需要选择一个值。
从昨天开始,我一直在努力实现这一目标,尽管还没有运气。我找到了解决方案,其中我想要完成的事情总是略有不同。
我试图获得所有可能的组合,有点像这样:combination_k,但我也希望相同的项目与自己配对,因此给出以下内容:
输入[1, 4, 5]和2(组合数)应返回:
[1, 1], [1, 4], [1, 5], [4, 4], [4, 5], [5, 5]
输入[1, 4, 5]并3应返回:
[1, 1, 1], [1, 1, 4], [1, 1, 5], [1, 4, 4], [1, 4, 5], [4, 4, 4], [4, 4, 5], [5, 5, 5], [5, 5, 4], [5, 5, 1] (顺序不重要)。
我一直在调整combination_k,它让我足够用2来工作,但是当我提供3作为参数时它不起作用。
const combinations = getAllCombinations([1, 4, 5], 2);
// combinations = [1, 1], [1, 4], [1, 5], [4, 4], …Run Code Online (Sandbox Code Playgroud) 如果某些值最终相等,我正在尝试使用合并值创建一个新的对象数组。
举个例子:
const arr1 = [{name: "Sven", firstName: "Alan", age: 24, profile: "Hello, i'm Alan"}, {name: "Mign", firstName: "Alex", age: 44, profile: "Hello, i'm Alex"}];
const arr2 = [{location: "Miami", alias: "ALAN", isAlive: true}, {location: "Los Angeles", alias: "Ethic", isAlive: true}]
Run Code Online (Sandbox Code Playgroud)
在这种情况下,第一个数组的 firstName 与第二个数组的别名匹配,在这种情况下,我想将它们合并为对象并像这样返回它们:
const newArray = [{name: "Sven", firstName: "Alan", age: 24, profile: "Hello, i'm Alan", isAlive: true, location: "Miami"}]
Run Code Online (Sandbox Code Playgroud)
我尝试了多种解决方案,但最接近的解决方案是:
const newArray = arr1.map((item, i) => {
const mapping = arr2.map(sym => sym.firstName.toLowerCase());
if (mapping.includes(item.alias.toLowerCase())) {
return Object.assign({}, …Run Code Online (Sandbox Code Playgroud) 我想模拟一个自动完成功能。一个句子应该用列表中的单词来完成。这些单词应在句子末尾逐个字符显示。
\n这是 HTML:
\n<p>We can help you with<span id="complete"></span></p>\nRun Code Online (Sandbox Code Playgroud)\n这是 JS 的一部分:
\nlet words = ["design", "frontend", "backend"];\nlet output = document.getElementById("complete");\nRun Code Online (Sandbox Code Playgroud)\n首先我尝试了这个:
\nwords.forEach((e) => {\n for (i = 0; i < e.length; i++) {\n setTimeout(() => {\n console.log(e[i]);\n }, 500);\n }\n});\nRun Code Online (Sandbox Code Playgroud)\n控制台记录:
\n\n我认为这是因为迭代器在setTimeout之前前进。
\n所以我尝试了这样的 while 循环:
\nwords.forEach((e) => {\n let i = 0;\n while (i <= e.length) {\n setTimeout(() => {\n console.log(e[i]);\n }, 600);\n i++;\n …Run Code Online (Sandbox Code Playgroud) 最近,我正在研究 Javascript 中的 Scope。我想知道自动提升是在编译时完成还是在执行代码时(运行时)完成。如果它在运行时确实如此,那么我还有另一个问题,自动提升是否会降低 Javascript 程序的性能。
something = a();
function a(){
console.log("hoisting");
return 10;
}
var something;
Run Code Online (Sandbox Code Playgroud)
是用人工吊装好还是自动吊装好?
javascript ×9
arrays ×4
hoisting ×2
typescript ×2
analytics ×1
css ×1
html ×1
json ×1
object ×1
splunk ×1