Ad

How To Extract A Single Value From An Array

- 1 answer

I am very new to JavaScript so forgive me if this is a dumb question:

I have this Ajax call:

$.ajax({
  type: 'GET',
  url: 'product_prices/' + value,
  success: function (data) {
    console.log('success', data)
  }
});

The value "data" produces an array:

success [{"price":"120.00"}]

What I need, is to extract the value of price (the 120) and use it later in an addition.

How do I get this value out?

Ad

Answer

Test it, You must parse JSON string before using it!

var data = JSON.parse('[{"price":"120.00"}]'); var Price = data[0].price; // 120.00 //OR IF it's Single And not Array var Price = data.price; // 120.00

Ad
source: stackoverflow.com
Ad