Как получить конкретный c объект из всего объекта? - PullRequest
0 голосов
/ 01 августа 2020

Я пытаюсь получить конкретный объект c из целого объекта. Ниже приведен пример использования 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}]

Можно ли это сделать каким-нибудь образом?

Ответы [ 3 ]

1 голос
/ 01 августа 2020

objc={"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"}]
}

res = objc["category"]
res.shift()
console.log(res)
1 голос
/ 01 августа 2020

const {category} = JSON .parse (localStorage.getItem ('shop / elasticCache / shirt');)

или

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"
            }]



            }
0 голосов
/ 01 августа 2020

$ (документ) .ready (function () {var jsonObj = {"description": "Tech Shirt", "configurable_options": [{"attribute_id": 80, "values": [{"value_index": " 5176 "," label ":" RUST "}]," label ":" Цвет "," attribute_code ":" color "}, {" attribute_id ": 125," values ​​": [{" value_index ":" 2898 " , "label": "Small"}, {"value_index": "2901", "label": "Medium"}, {"value_index": "2904", "label": "Large"}], "label" : «Размер», «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": "Категория по умолчанию", "position": 1}, { "category_id": 3, "name": "Одежда", "position": 14985}, {"category_id": 30, "name": "Низ", "position": 798}, {"category_id": 58, "name": "Леггинсы", "position": 1}, {"category_id": 1130, "name": "Char Test Category", "position": 30}], "sku": "S155551", "product_links" ": [{" link_ty pe ":" related "," connected_product_sku ": null," connected_product_type ": null," position ": 0," sku ":" P100031 "}, {" link_type ":" related "," connected_product_sku ": null," connected_product_type ": null," position ": 0," sku ":" P100031 "}]

            };
            for(var i = 1; i < jsonObj.category.length; i++){
                console.log(jsonObj.category[i]);
            }
        });

Вы можете перебирать весь объект и получать требуемые значения. Приведенный выше код дает значения категории из индекса 1.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...