Как найти объект в объекте json?

Добрый день. В общем, суть такая. В каталоге на woocommerce в витрине вывожу перечень вариация товара. При нажатии на вариацию должна происходить смена артикула и стоимости. Нашел в форме покупки такой атрибут data-product_variations. В нем содержится такой объект
[{
    "attributes": {
        "attribute_pa_czvet": "czvet-3"
    },
    "availability_html": "",
    "backorders_allowed": false,
    "dimensions": {
        "length": "",
        "width": "",
        "height": ""
    },
    "dimensions_html": "\u041d\/\u0414",
    "display_price": 220,
    "display_regular_price": 220,
    "image": {
        "title": "pic-goods-2",
        "caption": "",
        "url": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2.jpg",
        "alt": "",
        "src": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2.jpg",
        "srcset": false,
        "sizes": "(max-width: 233px) 100vw, 233px",
        "full_src": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2.jpg",
        "full_src_w": 233,
        "full_src_h": 192,
        "gallery_thumbnail_src": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2-100x100.jpg",
        "gallery_thumbnail_src_w": 100,
        "gallery_thumbnail_src_h": 100,
        "thumb_src": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2.jpg",
        "thumb_src_w": 233,
        "thumb_src_h": 192,
        "src_w": 233,
        "src_h": 192
    },
    "image_id": "90",
    "is_downloadable": false,
    "is_in_stock": true,
    "is_purchasable": true,
    "is_sold_individually": "no",
    "is_virtual": false,
    "max_qty": "",
    "min_qty": 1,
    "price_html": "<span class=\"price\"><span class=\"woocommerce-Price-amount amount\">220&nbsp;<span class=\"woocommerce-Price-currencySymbol\">\u0440\u0443\u0431.<\/span><\/span><\/span>",
    "sku": "M-02Q9",
    "variation_description": "",
    "variation_id": 158,
    "variation_is_active": true,
    "variation_is_visible": true,
    "weight": "",
    "weight_html": "\u041d\/\u0414"
}, {
    "attributes": {
        "attribute_pa_czvet": "czvet-4"
    },
    "availability_html": "",
    "backorders_allowed": false,
    "dimensions": {
        "length": "",
        "width": "",
        "height": ""
    },
    "dimensions_html": "\u041d\/\u0414",
    "display_price": 287,
    "display_regular_price": 287,
    "image": {
        "title": "pic-goods-2",
        "caption": "",
        "url": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2.jpg",
        "alt": "",
        "src": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2.jpg",
        "srcset": false,
        "sizes": "(max-width: 233px) 100vw, 233px",
        "full_src": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2.jpg",
        "full_src_w": 233,
        "full_src_h": 192,
        "gallery_thumbnail_src": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2-100x100.jpg",
        "gallery_thumbnail_src_w": 100,
        "gallery_thumbnail_src_h": 100,
        "thumb_src": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2.jpg",
        "thumb_src_w": 233,
        "thumb_src_h": 192,
        "src_w": 233,
        "src_h": 192
    },
    "image_id": "90",
    "is_downloadable": false,
    "is_in_stock": true,
    "is_purchasable": true,
    "is_sold_individually": "no",
    "is_virtual": false,
    "max_qty": "",
    "min_qty": 1,
    "price_html": "<span class=\"price\"><span class=\"woocommerce-Price-amount amount\">287&nbsp;<span class=\"woocommerce-Price-currencySymbol\">\u0440\u0443\u0431.<\/span><\/span><\/span>",
    "sku": "M-02Q9",
    "variation_description": "",
    "variation_id": 159,
    "variation_is_active": true,
    "variation_is_visible": true,
    "weight": "",
    "weight_html": "\u041d\/\u0414"
}, {
    "attributes": {
        "attribute_pa_czvet": "czvet-5"
    },
    "availability_html": "",
    "backorders_allowed": false,
    "dimensions": {
        "length": "",
        "width": "",
        "height": ""
    },
    "dimensions_html": "\u041d\/\u0414",
    "display_price": 215,
    "display_regular_price": 215,
    "image": {
        "title": "pic-goods-2",
        "caption": "",
        "url": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2.jpg",
        "alt": "",
        "src": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2.jpg",
        "srcset": false,
        "sizes": "(max-width: 233px) 100vw, 233px",
        "full_src": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2.jpg",
        "full_src_w": 233,
        "full_src_h": 192,
        "gallery_thumbnail_src": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2-100x100.jpg",
        "gallery_thumbnail_src_w": 100,
        "gallery_thumbnail_src_h": 100,
        "thumb_src": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2.jpg",
        "thumb_src_w": 233,
        "thumb_src_h": 192,
        "src_w": 233,
        "src_h": 192
    },
    "image_id": "90",
    "is_downloadable": false,
    "is_in_stock": true,
    "is_purchasable": true,
    "is_sold_individually": "no",
    "is_virtual": false,
    "max_qty": "",
    "min_qty": 1,
    "price_html": "<span class=\"price\"><span class=\"woocommerce-Price-amount amount\">215&nbsp;<span class=\"woocommerce-Price-currencySymbol\">\u0440\u0443\u0431.<\/span><\/span><\/span>",
    "sku": "M-02Q9",
    "variation_description": "",
    "variation_id": 160,
    "variation_is_active": true,
    "variation_is_visible": true,
    "weight": "",
    "weight_html": "\u041d\/\u0414"
}, {
    "attributes": {
        "attribute_pa_czvet": "czvet-2"
    },
    "availability_html": "",
    "backorders_allowed": false,
    "dimensions": {
        "length": "",
        "width": "",
        "height": ""
    },
    "dimensions_html": "\u041d\/\u0414",
    "display_price": 235,
    "display_regular_price": 235,
    "image": {
        "title": "pic-goods-2",
        "caption": "",
        "url": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2.jpg",
        "alt": "",
        "src": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2.jpg",
        "srcset": false,
        "sizes": "(max-width: 233px) 100vw, 233px",
        "full_src": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2.jpg",
        "full_src_w": 233,
        "full_src_h": 192,
        "gallery_thumbnail_src": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2-100x100.jpg",
        "gallery_thumbnail_src_w": 100,
        "gallery_thumbnail_src_h": 100,
        "thumb_src": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2.jpg",
        "thumb_src_w": 233,
        "thumb_src_h": 192,
        "src_w": 233,
        "src_h": 192
    },
    "image_id": "90",
    "is_downloadable": false,
    "is_in_stock": true,
    "is_purchasable": true,
    "is_sold_individually": "no",
    "is_virtual": false,
    "max_qty": "",
    "min_qty": 1,
    "price_html": "<span class=\"price\"><span class=\"woocommerce-Price-amount amount\">235&nbsp;<span class=\"woocommerce-Price-currencySymbol\">\u0440\u0443\u0431.<\/span><\/span><\/span>",
    "sku": "RG-748",
    "variation_description": "<p>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 1002<\/p>\n",
    "variation_id": 149,
    "variation_is_active": true,
    "variation_is_visible": true,
    "weight": "",
    "weight_html": "\u041d\/\u0414"
}, {
    "attributes": {
        "attribute_pa_czvet": "czvet-1"
    },
    "availability_html": "",
    "backorders_allowed": false,
    "dimensions": {
        "length": "",
        "width": "",
        "height": ""
    },
    "dimensions_html": "\u041d\/\u0414",
    "display_price": 240,
    "display_regular_price": 240,
    "image": {
        "title": "pic-goods-2",
        "caption": "",
        "url": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2.jpg",
        "alt": "",
        "src": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2.jpg",
        "srcset": false,
        "sizes": "(max-width: 233px) 100vw, 233px",
        "full_src": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2.jpg",
        "full_src_w": 233,
        "full_src_h": 192,
        "gallery_thumbnail_src": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2-100x100.jpg",
        "gallery_thumbnail_src_w": 100,
        "gallery_thumbnail_src_h": 100,
        "thumb_src": "http:\/\/wp.elmar\/wp-content\/uploads\/2020\/03\/pic-goods-2.jpg",
        "thumb_src_w": 233,
        "thumb_src_h": 192,
        "src_w": 233,
        "src_h": 192
    },
    "image_id": "90",
    "is_downloadable": false,
    "is_in_stock": true,
    "is_purchasable": true,
    "is_sold_individually": "no",
    "is_virtual": false,
    "max_qty": "",
    "min_qty": 1,
    "price_html": "<span class=\"price\"><span class=\"woocommerce-Price-amount amount\">240&nbsp;<span class=\"woocommerce-Price-currencySymbol\">\u0440\u0443\u0431.<\/span><\/span><\/span>",
    "sku": "R-0",
    "variation_description": "<p>\u0412\u0430\u0440\u0438\u0430\u0446\u0438\u044f<\/p>\n",
    "variation_id": 148,
    "variation_is_active": true,
    "variation_is_visible": true,
    "weight": "",
    "weight_html": "\u041d\/\u0414"
}]

При нажатии на атрибут, я могу получить это значение attribute_pa_czvet. Как, к примеру, подставив значение к ключу attribute_pa_czvet найти стоимость, артикул? Вопрос такой: как найти ключ по значению другого ключа?
  • Вопрос задан
  • 63 просмотра
Пригласить эксперта
Ответы на вопрос 1
@MagicMight
let source = {...};  // Объект, в котором ищется ключ
let keyValue = '...';  // Значение ключа, который должен быть найден
let foundKey;  // Имя ключа, если он был найден по значению, иначе undefined

for( key of Object.keys( source ) ) {
   if( source[key] === keyValue ) {
      foundKey = key;
      break;
   }
}


UPD Вариант не подходит для глубокого поиска ключа по значению
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы