我正在尝试通过破坏分配方法来传递功能组件道具。
在下面的示例中,我尝试使用此概念,但是它不起作用。此代码的结果返回空,并且不打印该道具。
import React from 'react';
import { render } from 'react-dom';
const App = ({ username: name }) => (<h1>{username}</h1>)
render(
<App name="Tom" />,
document.getElementById('root')
);
Run Code Online (Sandbox Code Playgroud)
有关如何处理此问题的任何想法?
我有下面的代码。在这种情况下,我有意尝试使用 forEach。
function check(arr, el) {
arr.forEach((element) => {
console.log(element)
if (element === el) {
return true
}
})
}
check([1, 2, 3, 4, 5], 3)
Run Code Online (Sandbox Code Playgroud)
我期望代码返回 true,因为 el 值 3 在数组中。但它返回未定义。我究竟做错了什么?
您好,我有一个车牌号BZ8345LK,想要转换为BZ 8345 LK(在char和number之间添加空格)。
我尝试使用此Regex,但无法正常工作,只能将带数字的第一个字符设为空格。例如BZ 8345LK,' LK'不能与数字保持空格。
var str = 'BZ8345LK';
str.replace(/[^0-9](?=[0-9])/g, '$& ');
# return BZ 8345LK, I want BZ 8345 LK
Run Code Online (Sandbox Code Playgroud) 我试图匹配一个字符串上的某个单词,并且只有当它不存在时,我才想使用OR |运算符来匹配另一个单词....但是该匹配忽略了...我如何确保行为原理:
const str = 'Soraka is an ambulance 911'
const regex = RegExp('('+'911'+'|'+'soraka'+')','i')
console.log(str.match(regex)[0]) // should get 911 insteadRun Code Online (Sandbox Code Playgroud)
我正在尝试将所有图像元素放在html页面上然后检查每个图像(如果width < height(纵向)然后旋转它).
问题是我的脚本不适用于每个图像,但对于所有图像,因为我通过"img"标签获取图像,然后我不知道如何应用单个图像的旋转.
是否可以在不必修改每个图像的标记的情况下执行此操作(我想避免为每个图像放置ID)?
<script>
$(document).ready(function() {
var imgs = document.getElementsByTagName("img");
for (var i = 0; i < imgs.length; i++) {
img = document.getElementsByTagName("img")[i];
var width = img.clientWidth;
var height = img.clientHeight;
if(width<height){
img.setAttribute('style','transform:rotate(90deg)'); //how do I apply this for each image?
}
else{
}
}
});
</script>
Run Code Online (Sandbox Code Playgroud) 我注意到MDN在start Parameter的描述中提到:
这个描述对于我删除元素非常有用:
删除第一个元素:
let foo = ["a", "b", "c"];
foo.splice(0,1) // "a" removed
console.log(foo);//["b", "c"]Run Code Online (Sandbox Code Playgroud)
删除最后一个元素:
let foo = ["a", "b", "c"];
foo.splice(-1,1) // "c" removed
console.log(foo);//["a", "b"]Run Code Online (Sandbox Code Playgroud)
现在让我们尝试添加元素
在第 1 处添加元素 :
let foo = ["a", "b", "c"];
foo.splice(0,0,"x") // "x" added to 1st
console.log(foo);//["x", "a", "b", "c"]Run Code Online (Sandbox Code Playgroud)
将元素添加到最后:
let foo = ["a", "b", "c"];
foo.splice(-1,0,"x")
// I expected to added "x" as …Run Code Online (Sandbox Code Playgroud)当我们使用独立的函数语句作为IIFE时,我们需要对其进行包装()以使其起作用
// IFFE
(function a(){
console.log('Hello')
}());
// IFFE
(function a(){
console.log('Hello')
})()Run Code Online (Sandbox Code Playgroud)
如果我们不包装()代码,则会产生语法错误
function a(){
console.log('Hello')
}()Run Code Online (Sandbox Code Playgroud)
但是,当我们将其用作函数表达式时,不需要用 ()
let a = function a(){
console.log('Hello')
}()Run Code Online (Sandbox Code Playgroud)
那么,为什么()要在将其用作函数语句时需要包装它呢?
我需要基于现有常量javascript对象中的某些键选择来创建数组
const EXISTING_CONSTANT_OBJECT = {
'fr': '10',
'es': '15'
'us': '10'
'uk': '7'
//and so on for many other iso country codes and UNPREDICTABLE key names
}
Run Code Online (Sandbox Code Playgroud)
我需要能够EXISTING_CONSTANT_OBJECT使用值等于的所有键创建一个数组(无需出于不变性原因而进行修改)10。
例如,预期输出为
object_to_create_arr = ["fr","us"]
Run Code Online (Sandbox Code Playgroud)
我尝试使用reduce但失败了。
注意:我可以使用ES6,通常更喜欢,因为它通常更简洁。
我基本上想取出数组中的第一个对象并获取它的名称。这里唯一的挑战是我试图在父对象中解构它:
const exampleObject = {
collection: [{
name: "First Object",
}, {
name: "Second Object",
}],
};
const {
collection: [firstObject: {
name
}]
} = exampleObject;
console.log(firstObject);Run Code Online (Sandbox Code Playgroud)
有可能吗?
我读到它来检查Map 中是否存在一个键,我们可以使用has方法我正在使用以下示例:
var mp = new Map();
mp["abc-def"] = 123;
if (mp.has("abc-def")) {
console.log("found");
} else {
console.log("not found");
}Run Code Online (Sandbox Code Playgroud)
关于为什么has方法在这里不起作用的任何建议?