【发布时间】:2017-01-18 00:51:00
【问题描述】:
我正在关注this tutorial 以“检测”数组对象中是否存在唯一数字。这个数字是一个字符串。
我习惯于在 Ruby on Rails 中使用 detect,所以我在 React (JavaScript) 中寻找等价物:
ES6:
...
// this will be "data" in "this.props.data"
data = [
{
id: 1,
order_id: "44",
name: "Some order number name",
},
{
id: 2,
order_id: "65",
...,
}
]
// let num = "44"; Just for this example
renderCreditNote(num){
if (num instanceof this.props.data) {
return num.map(function(p){
return p.order_id
});
}
}
render(){
return({this.renderCreditNote().bind(this)})
}
...
实际上,这看起来像这样:this.renderCreditNote(this.state.num).bind(this)
因此,如果此 num 在数组中,则仅显示该数组。我这样做对吗?错误是:
未捕获的类型错误:'instanceof' 的右侧不可调用
【问题讨论】:
-
看起来你需要array.includes,而不是
instanceof。 -
array.includes 如何处理数组/对象?我知道如果你有一个整数列表它可以工作,但它是否处理检查嵌套对象?
-
Sylar,如果我理解正确,
this.props.data是一个什么数组?你想 1. 检查num是否在该数组中,如果它返回它的order_id? -
我在移动设备上。我会在一小时内更新问题
-
好的,我已经进行了编辑以显示我所拥有的。因此,如果订单 id 为 44,则仅返回该数组。
标签: javascript arrays reactjs