Javascript 创建与接收到的json具有相同结构的对象
Hola开发者我正试图在我的购物卡中创建一个产品,但我仍然存在一个问题: 找不到正确的方法来设计对象,就像我从json(后端)接收对象一样。 假设导致问题的零件是:Javascript 创建与接收到的json具有相同结构的对象,javascript,vue.js,vuex,Javascript,Vue.js,Vuex,Hola开发者我正试图在我的购物卡中创建一个产品,但我仍然存在一个问题: 找不到正确的方法来设计对象,就像我从json(后端)接收对象一样。 假设导致问题的零件是: JSON RECEIVED "product_category": [ { "categories_of_product": "Good" }, { "categories_of_product": "Danger" },
JSON RECEIVED
"product_category": [
{
"categories_of_product": "Good"
},
{
"categories_of_product": "Danger"
},
{
"categories_of_product": "Homer"
}
],
"people_buying_this_product": "Jack Ripper"
},
]
然后,在我的建筑产品流程中,数据返回是一个与此相关的部分,在该部分中,使用复选框,我会得到哪个复选框被选中或未选中,以便构建一个类别数组,类似于我之前显示的类别:
DATA RETURN
ProductAdded: {
description: "",
upload_image3: "",
upload_image2: "",
upload_image1: "",
unities: 0,
price: 0,
name: "",
Categories: [
{ id: 1, value: "Miscellaneous", selected: false },
{ id: 2, value: "Homer", selected: false },
{ id: 3, value: "Electronic", selected: false },
{ id: 4, value: "Internet", selected: true },
{ id: 5, value: "Kids", selected: false },
{ id: 6, value: "Donas", selected: true },
{ id: 7, value: "Sports", selected: true },
{ id: 8, value: "Horror", selected: false }
],
METHOD that dispatches de action in vuex
addProductOnSale(thisCurrent) {
this.$store.dispatch("addProductSale", this.ProductAdded);
}
然后,由于已经在VUEX状态管理中,在实际操作中,尝试为该产品类别构建新产品,我设置了以下内容:
addProductSale({commit,getters},currentProduct){
product_category: currentProduct.Categories.filter(option =>
option.selected).map(option => {categories_of_product: option.value})
}
------------------->这个给了我一个未定义的例子:
"product_category":
[
undefined
],
"product_category":
[
undefined
],
"product_category":
[
undefined
],
]
或者试过另一个:
addProductSale({commit,getters},currentProduct){
product_category:currentProduct.Categories.forEach( option.selected,() =>
option.value).map(option => option.selected),...
}
给了我一个错误,甚至没有显示结果。
基本上找不到为json设计相同结构的方法,甚至无法达到值。
这很奇怪,但如果我只设置如下查询:
addProductSale({commit,getters},currentProduct){
product_category:currentProduct.Categories.filter(option => option.selected)
}
事实上,过滤并返回类别内的对象,这些类别提交选定true的条件,但其简单格式与数据返回的格式相同,但这不是目的。
有什么建议或帮助吗?
提前感谢 要从JSON创建对象,请使用
currentProduct.Categories
应该是currentProduct.ProductAdded.Categories
{undefined}
是不可能的,{}
中的所有内容都是键:值
的形式,是代码的一部分,但基本上这是在vuex中构建的,ProductAdded在组件的分派方法中设置为参数要从JSON创建对象,请使用currentProduct.Categories
应该是currentProduct.ProductAdded.Categories
{undefined}
不可能,里面的所有内容都是键:值
的形式,我保留了部分代码,但基本上这是在vuex中构建的,ProductAdded在组件的分派方法中设置为参数