我有一个错误,我的obj.push()和obj.concat()不是一个函数,但我不太确定为什么。这是我的代码:
onSearch = () => {
var obj = {
product: [
{
field: "is_published",
filter_value: 1
},
{
field: "order_mode",
filter_array: [
"fcfs",
"purchase-order"
]
},
{
relationship: "store",
filter_object: {
field: "slug",
filter_value: "sample"
}
}
]
}
if (this.state.search !== "") {
obj.push(
{
field: "name",
text_search: this.state.search
}
)
}
var obj2 = {
taxonomies: [
[
{ field: "type",
filter_value: "seller"
},
{ field: "slug",
filter_value: "brand"
}
]
]
}
var conc = obj.concat(obj2);
var { getProductSearch } = this.props
getProductSearch(obj.concat(obj2))
}product和taxonomies存储在不同的变量中,但我需要将它们作为一个数组传递给getProductSearch,为此,我需要使用concat()。然后我需要使用push(),因为我想向数组obj中添加一个对象。我做错了什么?
发布于 2018-11-05 14:28:23
答案很简单,因为你不能推到一个物体上。Push用于数组。。
要完成这项工作,您可以更改代码,通过以下操作将代码推到对象中的数组上。
obj.product.push(
{
field: "name",
text_search: this.state.search
}
)如果你试图在水果、蔬菜、肉类等多种产品的情况下使产品具有活力,那么你可以通过简单的操作来改变它。
onSearch(productName)
obj[productName].push(
{
field: "name",
text_search: this.state.search
}
)这将允许您调用onSearch(蔬菜),并且只有在这样设置的情况下才能将其推送到该数组。
https://stackoverflow.com/questions/53156231
复制相似问题