Javascript 如何从整个对象中获取特定对象?
我试图从一个完整的对象中获取特定的对象,下面是一个示例 通过使用Javascript 如何从整个对象中获取特定对象?,javascript,Javascript,我试图从一个完整的对象中获取特定的对象,下面是一个示例 通过使用localStorage.getItem('shop/elasticCache/shirt') 我得到以下数据 {"description":"Tech Shirt","configurable_options":[{"attribute_id":80,"values":[{"value_index":"
localStorage.getItem('shop/elasticCache/shirt')代码>
我得到以下数据
{"description":"Tech Shirt","configurable_options":[{"attribute_id":80,"values":[{"value_index":"5176","label":"RUST"}],"label":"Color","attribute_code":"color"},{"attribute_id":125,"values":[{"value_index":"2898","label":"Small"},{"value_index":"2901","label":"Medium"},{"value_index":"2904","label":"Large"}],"label":"Size","attribute_code":"size"}],"tsk":1594790209,"size_options":[2898,2901,2904],"regular_price":28,"final_price":null,"price":28,"color_options":[5176],"special_from_date":null,"id":250659,"category":[{"category_id":2,"name":"Default Category","position":1},{"category_id":3,"name":"Clothing","position":14985},{"category_id":30,"name":"Bottoms","position":798},{"category_id":58,"name":"Leggings","position":1},{"category_id":1130,"name":"Char Test Category","position":30}],"sku":"S155551","product_links":[{"link_type":"related","linked_product_sku":null,"linked_product_type":null,"position":0,"sku":"P100031"},{"link_type":"related","linked_product_sku":null,"linked_product_type":null,"position":0,"sku":"P100031"}
我正试着去
{"category_id":3,"name":"Clothing","position":14985},{"category_id":30,"name":"Bottoms","position":798},{"category_id":58,"name":"Leggings","position":1},{"category_id":1130,"name":"Char Test Category","position":30}]
他们有什么方法可以做到吗?const{category}=JSON.parse(localStorage.getItem('shop/elasticCache/shirt');)
或
objc={“描述”:“技术衬衫”,“可配置的_选项”:
[{“属性id”:80,“值”:[{“值索引”:“5176”,“标签”:“锈”}],
“标签”:“颜色”,“属性代码”:“颜色”},
{“attribute_id”:125,“values”:[{“value_index”:“2898”,“label”:“Small”}
,{“value_index”:“2901”,“label”:“Medium”},
{“value_index”:“2904”,“label”:“Large”}],
“标签”:“大小”,“属性代码”:“大小”}],
“tsk”:1594790209,“尺寸选项”:[289829012904]
,“常规价格”:28,“最终价格”:空,“价格”:28,
“颜色选项”:[5176],“从日期开始的特殊选项”:null,“id”:250659,
“类别”:[{“类别id”:2,“名称”:“默认类别”,“位置”:1},
{“类别id”:3,“名称”:“服装”,“职位”:14985},
{“category_id”:30,“name”:“Bottoms”,“position”:798},
{“类别id”:58,“名称”:“绑腿”,“位置”:1},
{“category_id”:1130,“name”:“Char Test category”,“position”:30}],
“sku”:“S155551”,“产品链接”:
[{“链接类型”:“相关”,“链接产品”sku:null,“链接产品”sku:null,“位置”:0,“sku”:“P100031”},
{“链接类型”:“相关”,“链接产品”sku:null,“链接产品”sku:null,“位置”:0,“sku”:“P100031”}]
}
res=objc[“类别”]
移位决议()
console.log(res)
$(文档).ready(函数(){
var jsonObj={
“描述”:“科技衬衫”,
“可配置_选项”:[{
“属性id”:80,
“价值观”:[{
“价值指数”:“5176”,
“标签”:“生锈”
}],
“标签”:“颜色”,
“属性代码”:“颜色”
}, {
“属性id”:125,
“价值观”:[{
“价值指数”:“2898”,
“标签”:“小”
}, {
“价值指数”:“2901”,
“标签”:“中等”
}, {
“价值指数”:“2904”,
“标签”:“大”
}],
“标签”:“尺寸”,
“属性代码”:“大小”
}],
“tsk”:1594790209,
“尺寸选项”:[28982901294],
“正常价格”:28,
“最终价格”:空,
“价格”:28,
“颜色选项”:[5176],
“从日期开始的特殊日期”:空,
“id”:250659,
“类别”:[{
“类别识别号”:2,
“名称”:“默认类别”,
“职位”:1
}, {
“类别识别号”:3,
“姓名”:“服装”,
“职位”:14985
}, {
“类别识别号”:30,
“名称”:“底部”,
“职位”:798
}, {
“类别识别号”:58,
“名称”:“绑腿”,
“职位”:1
}, {
“类别识别号”:1130,
“名称”:“字符测试类别”,
“职位”:30
}],
“sku”:“S155551”,
“产品链接”:[{
“链接类型”:“相关”,
“链接产品库存单位”:空,
“链接产品类型”:空,
“位置”:0,
“sku”:“P100031”
}, {
“链接类型”:“相关”,
“链接产品库存单位”:空,
“链接产品类型”:空,
“位置”:0,
“sku”:“P100031”
}]
};
对于(var i=1;i
您可以遍历整个对象并获得所需的值。上面的代码给出了索引1中category的值。这还返回{“category_id”:2,“name”:“Default category”,“position”:1}
,因此它可能应该是const category=JSON.parse(localStorage.getItem('shop/elasticCache/shirt')).category.slice(1)代码>我有console.log,但是它的未定义的代码将给你类别数组作为响应。您需要的第一个元素是什么?我可以在“const{category}的数据中设置一些键值吗?我已将您的答案标记为该问题的正确答案。
const {category } = {
"description": "Tech Shirt",
"configurable_options": [{
"attribute_id": 80,
"values": [{
"value_index": "5176",
"label": "RUST"
}],
"label": "Color",
"attribute_code": "color"
}, {
"attribute_id": 125,
"values": [{
"value_index": "2898",
"label": "Small"
}, {
"value_index": "2901",
"label": "Medium"
}, {
"value_index": "2904",
"label": "Large"
}],
"label": "Size",
"attribute_code": "size"
}],
"tsk": 1594790209,
"size_options": [2898, 2901, 2904],
"regular_price": 28,
"final_price": null,
"price": 28,
"color_options": [5176],
"special_from_date": null,
"id": 250659,
"category": [{
"category_id": 2,
"name": "Default Category",
"position": 1
}, {
"category_id": 3,
"name": "Clothing",
"position": 14985
}, {
"category_id": 30,
"name": "Bottoms",
"position": 798
}, {
"category_id": 58,
"name": "Leggings",
"position": 1
}, {
"category_id": 1130,
"name": "Char Test Category",
"position": 30
}],
"sku": "S155551",
"product_links": [{
"link_type": "related",
"linked_product_sku": null,
"linked_product_type": null,
"position": 0,
"sku": "P100031"
}, {
"link_type": "related",
"linked_product_sku": null,
"linked_product_type": null,
"position": 0,
"sku": "P100031"
}]
}
};
for(var i = 1; i < jsonObj.category.length; i++){
console.log(jsonObj.category[i]);
}
});