有人请解释以下内容:
我跟随丹阿布拉莫夫的讲座和做练习.
代码工作正常,但是当使用大括号编写以下特定函数时,测试失败**{ }**.
case 'toggleTodo' :
return (
state.map( (one) => {
oneTodo( one, action )
})
);
Run Code Online (Sandbox Code Playgroud)
相同的代码工作正常,没有花括号.
case 'toggleTodo' :
return (
state.map( (one) =>
oneTodo( one, action )
)
);
Run Code Online (Sandbox Code Playgroud)
这是JsBin.请参阅第31行.
javascript brackets curly-braces ecmascript-6 arrow-functions
我发现了一个奇怪的问题.
给定一个过滤器和一个对象数组,我想只选择那些与过滤器匹配的对象.
奇怪的是,这不起作用
this.state.articles.filter((article) => {
article.category === filter
})
Run Code Online (Sandbox Code Playgroud)
虽然这样做
this.state.articles.filter((article) => article.category === filter )
Run Code Online (Sandbox Code Playgroud)
我原本以为他们会评价相同,但似乎并非如此.有什么想法吗?
我在代码中遇到了一个小问题,这让我感到困惑,并希望有人可以解释为什么会这样做.
代码1
sendText(){
return this.http.get('/api')
.map((response:Response) => response.json());
}
Run Code Online (Sandbox Code Playgroud)
代码2
sendText(){
return this.http.get('/api').map((response:Response) => {
response.json();
});
}
Run Code Online (Sandbox Code Playgroud)
这两个代码之间的关键区别在于,在代码2中,我在箭头函数之后放置了括号,将我的任务添加到这些括号中,在代码1中,我将括号取出并将任务放在一行上.
我的问题是,为什么不从服务器端回来为未定义在未来我的目标代码2与angular2而提供的订阅方法代码1返回我怀疑的对象.
在箭头函数中返回值与添加正文并键入 return 之间有什么区别吗?
据我所知,它们是一样的。
这是一个会话:
let a = () => 1;
a()
1
let b = () => { return 1; }
b()
1
a
() => 1
b
() => { return 1; }
Run Code Online (Sandbox Code Playgroud)
当这些不同时有什么情况吗?
即时通讯在React应用程序中使用redux。为什么此过滤函数会改变原始state.product?我不明白为什么
state.products = [
{
type: "one",
products: [
{ active: true },
{ active: false }
]
}
]
function mapStateToProps(state) {
const test = state.products.filter((item) => {
if(item.type === "one") {
return item.products = item.products.filter((item) => {
item.active
});
}
return item;
});
return {
machineSearchWeightRange: state.machineSearchWeightRange,
filteredItems: test //This will have only products active
};
}
Run Code Online (Sandbox Code Playgroud)
filteredItems将仅具有活动产品,但state.products也将被更新,仅在尝试再次过滤同一数据时仅包含活动产品。
意见建议
我的问题与此类似,但不一样。
我正在尝试使用对对象数组进行排序localeCompare。它没有理由不工作,但它不工作。不知怎的,我正在做正确的事情。/叹气/
这就是我获取数据的方式
const [data, setData] = useState([]);
const [isBusy, setBusy] = useState(true)
useEffect(() => {
console.log('I was run once');
async function fetchData() {
const url = `${
process.env.REACT_APP_API_BASE
}/api/v1/endpoint/`;
axios.get(url).then((response: any) => {
setBusy(false);
setData(response.data.results)
console.log(response.data.results);
});
}
fetchData();
}, [])
Run Code Online (Sandbox Code Playgroud)
这是排序功能
function onSort(event: any, sortKey: string) {
const newData = data;
newData.sort((a: any, b: any): any => {
a[sortKey].toString().localeCompare(b[sortKey]);
})
setData(newData);
}
Run Code Online (Sandbox Code Playgroud)
典型的对象数组是这样的
[
{
"avatar": "big",
"name": "Username",
},
{
"avatar": "small",
"name": …Run Code Online (Sandbox Code Playgroud) 为什么这样做:
const final = pdata.map((p) => p.nodeName);
// returns [ 'H1', 'P', 'P' ] like its supposed to
Run Code Online (Sandbox Code Playgroud)
但这在所有这些中都返回 undefined :
const final = pdata.map((p) => {
p.nodeName
});
// returns [ undefined, undefined, undefined ]
Run Code Online (Sandbox Code Playgroud)
我需要在里面添加几个 if 语句来检查不同的类型,但{}似乎破坏了它。我不应该在 a 中进行此操作.map()吗?或者有另一种方法可以做到这一点?
不清楚为什么当有9个空字符串时,这会返回0.
function isEmpty(){
const emptySlots = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '].filter(slot => { slot === " "})
return emptySlots.length === 9; // emptySlots.length is 0 for some reason
}
Run Code Online (Sandbox Code Playgroud) 所以据我所知,此时此刻显然是错误的,
return arg => arg*2
Run Code Online (Sandbox Code Playgroud)
是相同的
return (arg)=>{arg*2}
Run Code Online (Sandbox Code Playgroud)
我一直认为箭头函数在语法上更简洁。
但是用像这样的闭包来做这件事是行不通的。
function addTwoDigits(firstDigit){
return (secondDigit)=>{firstDigit + secondDigit}
}
let closure = addTwoDigits(5);
console.log(closure(5)) // Undefined
Run Code Online (Sandbox Code Playgroud)
然而这很好
function addTwoDigitsV2(firstDigit){
return secondDigit => firstDigit + secondDigit
}
let closure2 = addTwoDigitsV2(10);
console.log(closure2(10))// 20
Run Code Online (Sandbox Code Playgroud) javascript ×10
ecmascript-6 ×5
reactjs ×2
angular ×1
brackets ×1
closures ×1
curly-braces ×1
filter ×1
function ×1
node.js ×1
redux ×1
return ×1