0

I am getting this json via ajax:

[{"currency_value":"1.00","currency_code":"47","type":"BUY"},{"currency_value":"1.00","currency_code":"47","type":"SELL"}]

However i want to use only the one who has the type of "SELL"

 var setcurrencyvalue = function(to, value) {
    parseInt($(to).attr('value', value)); // set the currency val
    calculatetotal();
    $('.amount-remaining').val(0);
    //set the amount
}

var showcurval = function(cur_code) {
    var $curcode = '';
    var $currency = $('.currency-czk');
    $.ajax({
       type: 'GET',
       url:  '/transactions/get_currency_val?cur_code=' + cur_code,
       success: function(data) {
           var obj = JSON.parse(data);
           $.each(obj, function (k, v) {
             //  console.log(v);
               switch (v.currency_code) {
                   case '47':
                       $curcode = '€'; // 1
                       setcurrencyvalue($currency, v.currency_value);
                       break;
                   case '33':
                       $curcode = 'Kč'; // CZK/EUR
                       setcurrencyvalue($currency, v.currency_value);
                       break;
                   case '131':
                       $curcode = '₽'; //CZK/RUB
                       setcurrencyvalue($currency, v.currency_value);
                       break;
                   case '144':
                       $curcode = '$'; //CZK/USD
                       setcurrencyvalue($currency, v.currency_value);
                       break;
                   case '168':
                       alert('Please set a currency!');
                       break;
               }

           });

       }
    });
}

This will give me the the v.currency_value randomly there are two of them. how to tell Ajax to work only with "SELL" or "BUY"?

Ilanus
  • 6,690
  • 5
  • 13
  • 37
  • If this is an ajax issue you need to check up the api, we don't know what that is. Otherwise it's your code at fault. – simonzack Nov 26 '15 at 15:45

4 Answers4

1

You can filter the data you get per AJAX.

var obj = JSON.parse(data);
var sells = obj.filter(function(a) {
    return a.type === "SELL";
});

This will return new array only with the objects that have the value SELL in their attribute type.

Here is also a link to the array method filter:

https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

cezar
  • 11,616
  • 6
  • 48
  • 84
1

Just filter the data when it's returned:

var obj = JSON.parse(data).filter(function (el) {
  return el.type === 'SELL';
})[0]; // { currency_value: "1.00", currency_code: "47", type: "SELL" }

filter returns an array. The [0] returns the first element of the array. Leave it off if you have more than one object with type: 'SELL' that you need to be returned.

If you wanted to have a generalised function to get objects by type from the data it might look like this:

function getDataByType(data, type) {
    return data.filter(function (el) {
        return el.type === type;
    });
}

var arr = getDataByType(data, 'SELL');

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95
1

You could filter the resulting array just after having parsed it. Basically its about using the filter prototype:

obj.filter(function (el) {
     return el.type === "SELL"
});

As a result you get a new array which only contains the elements that pass the filter-function.

For more information on filtering JS-Arrays see this one:

Javascript: How to filter object array based on attributes?

Cheers, Kristof

Community
  • 1
  • 1
K. Berger
  • 361
  • 1
  • 9
0

You can have a if condition like this

 $.ajax({
   type: 'GET',
   url:  '/transactions/get_currency_val?cur_code=' + cur_code,
   success: function(data) {
       var obj = JSON.parse(data);
       $.each(obj, function (k, v) {
         //  console.log(v);
         if(v.type == "SELL"){
           switch (v.currency_code) {
               case '47':
                   $curcode = '€'; // 1
                   setcurrencyvalue($currency, v.currency_value);
                   break;
               case '33':
                   $curcode = 'Kč'; // CZK/EUR
                   setcurrencyvalue($currency, v.currency_value);
                   break;
               case '131':
                   $curcode = '₽'; //CZK/RUB
                   setcurrencyvalue($currency, v.currency_value);
                   break;
               case '144':
                   $curcode = '$'; //CZK/USD
                   setcurrencyvalue($currency, v.currency_value);
                   break;
               case '168':
                   alert('Please set a currency!');
                   break;
           }
         }
       });

   }
});
Amin Kodaganur
  • 646
  • 6
  • 19