jquery - 从 json 对象中分离一些值(react,jquery)
问题描述
在反应的应用程序中,我有这个 json 数据来自var product
我正在尝试从 json 下面存储“类别名称”
喜欢
category: 'Jumpsuits,Cargo Jumpsuit' // this is the desired output
product = {
"isConfigurableProduct": true,
"id": "bdg-workwear-denim-boiler-suit",
"categories": [
{
"id": 2437,
"name": "Jumpsuits",
"url_key": "jumpsuits",
"level": 4,
"url_suffix": ".html",
"breadcrumbs": [
{
"category_id": 3,
"category_level": 2,
"category_name": "Clothing",
"category_url_key": "clothing",
"category_url_path": "clothing"
},
{
"category_id": 82,
"category_level": 3,
"category_name": "Jumpsuits & Rompers",
"category_url_key": "jumpsuits-rompers",
"category_url_path": "clothing/jumpsuits-rompers"
}
],
"path": "clothing/jumpsuits-rompers"
},
{
"id": 2492,
"name": "Cargo Jumpsuit",
"url_key": "cargo-jumpsuit",
"level": 5,
"breadcrumbs": [
{
"category_id": 3,
"category_name": "Clothing",,
"category_url_path": "clothing"
},
{
"category_id": 82,
"category_name": "Jumpsuits & Rompers",
"category_url_path": "clothing/jumpsuits-rompers"
},
{
"category_id": 2437,
"category_name": "Jumpsuits",
"category_url_key": "jumpsuits",
}
],
"path": "clothing/jumpsuits-rompers/jumpsuits"
}
]
}
他们有什么办法可以将它与这个 json 分开吗?获取它的优化方式是什么?任何想法,感谢
解决方案
您可以迭代并将类别名称推送到单独的数组并将它们分开。-
const product = {
"isConfigurableProduct": true,
"id": "bdg-workwear-denim-boiler-suit",
"categories": [
{
"id": 2437,
"name": "Jumpsuits",
"url_key": "jumpsuits",
"level": 4,
"url_suffix": ".html",
"breadcrumbs": [
{
"category_id": 3,
"category_level": 2,
"category_name": "Clothing",
"category_url_key": "clothing",
"category_url_path": "clothing"
},
{
"category_id": 82,
"category_level": 3,
"category_name": "Jumpsuits & Rompers",
"category_url_key": "jumpsuits-rompers",
"category_url_path": "clothing/jumpsuits-rompers"
}
],
"path": "clothing/jumpsuits-rompers"
},
{
"id": 2492,
"name": "Cargo Jumpsuit",
"url_key": "cargo-jumpsuit",
"level": 5,
"breadcrumbs": [
{
"category_id": 3,
"category_name": "Clothing",
"category_url_path": "clothing"
},
{
"category_id": 82,
"category_name": "Jumpsuits & Rompers",
"category_url_path": "clothing/jumpsuits-rompers"
},
{
"category_id": 2437,
"category_name": "Jumpsuits",
"category_url_key": "jumpsuits",
}
],
"path": "clothing/jumpsuits-rompers/jumpsuits"
}
]}
let sub_category_names = []
let category_names = []
product.categories.forEach(function (category) {
category_names.push(category.name)
category.breadcrumbs.forEach(function (item) {
sub_category_names.push(item.category_name)
});
});
// for convert to string
let category_str = category_names.toString();
let str = sub_category_names.toString();
console.log("Main Categories are",category_str);
console.log("Sub Cateogries are ",str);
推荐阅读
- database - Scala 从 oracle 表中获取详细信息并在 hive 表中触发查询
- spring - 如何防止 Spring Boot Web 应用程序在启动时接受 http 请求?
- loops - 如何制作一个for循环来从firebase中获取链接?
- bash - 使用 sudo 运行命令时,我不断收到权限被拒绝错误
- reactjs - 按特定键过滤对象数组
- vue.js - 在vue js中如何使用通过插槽呈现的复选框调用子组件方法
- android - 为 ARM:./.libs/libprotoc.so 交叉编译 protobuf 时出错:error: undefined reference to 'descriptor
- spring - 如何设置@value属性java
- numpy - 如何控制嵌套参数的解包深度并将它们作为单独的参数传递给 Python 函数?
- python - 如何在运行的工件内的 mlflow 中记录自定义模型?