S
S
setevoi112015-11-15 22:09:06
JavaScript
setevoi11, 2015-11-15 22:09:06

How to get values ​​from json correctly?

How to correctly get values ​​from json

product:  {
  "id":48597847,
  "price_kinds":[
    {"id":11467,"price_index":1,"value":2},
    {"id":11468,"price_index":2,"value":5}
    ],
  "variants":[
    {"prices":[1290.0,999.0],"price":"1490.0","price2":1290.0,"price3":999.0}
    ]
  },


I managed to get the price_kind values, but then I do something obviously wrong

$(function(){
    var
      $config = {
        product:  {{ product | json }},
      };
        var
        self        = this,
        prices     = [],
        price_kinds = [];
    
    Events( 'onQuantity_Change' ).subscribe( function( $data ){
      // вытаскиваем правила для типов цен
      $.each( $config.product.price_kinds, function( index, $price_kind ) {
        price_kinds[ $price_kind.price_index ] = $price_kind.value;
      });
      self.price_kinds = price_kinds; 

    // массив цена правило
    // дальше не работает 
      prices[ 0 ] = {
        price: parseFloat( $config.product.price ),
        value: 0,
      };      
      
      $.each( $config.product.variants, function( index, price ){
        prices[ index +1 ] = {
          price: price,
          value: self.price_kinds[ index +1 ],
        };
     });

      self.prices = prices;

      console.log( 'onBuyButton_Active: ', self.price_kinds );
      console.log( 'onBuyButton_Active: ', self.prices );
    });

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Arman, 2015-11-15
@setevoi11

Something is not visible $config.product.price - price is not there, maybe like this: it's
better to look so where you have or lose

console.log($config);
console.log($config.product);
console.log($config.product.variants);

V
Valera Karmanov, 2015-11-15
@motokraft

How to correctly get values ​​from json

Are you sure that this is a JSon code, and indeed where did you get it from, if you took it from somewhere, then php will not make such a terrible code...

S
setevoi11, 2015-11-15
@setevoi11

Thank you. Yes, that's exactly right

$config.product.variants[0].price
$config.product.variants[0].prices

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question