4

I want to fetch only 1st element of json array

my json data :

{  
 id:"1",
 price:"130000.0",
 user:55,
}
{  
  id:"2",
  price:"140000.0",
 user:55,
}

i want to access the price of 1st json element price : "13000.0"

my code

$.each(data_obj, function(index, element) {
    $('#price').append(element.price[0]);
});

but my output is '1'

rahul.m
  • 5,572
  • 3
  • 23
  • 50
  • Possible duplicate of [How to access first element of JSON object array?](https://stackoverflow.com/questions/29032525/how-to-access-first-element-of-json-object-array) – Anuga Sep 28 '18 at 12:44
  • Try 'element.price' instead of 'element.price[0]', that splits the price in an array, which is what your code does, you get the first character of the price which is '1'. – NLxDoDge Sep 28 '18 at 12:46

8 Answers8

4

Assuming that you have array of objects

var arr = [{  
      id:"1",
      price:"130000.0",
      user:55,
     },
     {  
       id:"2",
       price:"140000.0",
      user:55,
     }]

     console.log(arr[0].price)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Ali Shahbaz
  • 825
  • 1
  • 7
  • 19
  • @c.grey I guess you have JSON string, you need to parse your JSON string to JSON first then you'll be able to get data according to my given example. For this, parse with `JSON.parse(data)` – Ali Shahbaz Sep 28 '18 at 13:01
  • I have parse the JSON data – rahul.m Sep 28 '18 at 17:28
4

You data isn't valid JSON, JSON data key must be wrap within double quote, but your data isn't wrapped in double quote

var data = [{  
    "id":"1",
    "price":"130000.0",
    "user":55
},{  
    "id":"2",
    "price":"140000.0",
    "user":55
}]

console.log(data[0]["price"]);
Yves Kipondo
  • 5,289
  • 1
  • 18
  • 31
1

Hello You just need to add [] from starting and ending point of your json string. see here var data = JSON.parse( '[{ "id":"1","price":"130000.0","user":55},{"id":"2","price":"140000.0","user":55}]');

var priceValue = 0;
$.each(data, function(index, element) {if(index == 0){  priceValue =    element.price;}});console.log(priceValue);

Your answer will be 13000.0

saumil_
  • 304
  • 2
  • 11
0

The element having the your JSON data means, we can able to use below code to get the first JSON data.

element[0].price

Thanks,

jawahar N
  • 462
  • 2
  • 13
0

You are using for each loop and in function you get 2 params first one is index and second is the element itself. So this will iterate through all elements.

$.each(data_obj, function(index, element) {
      $('#price').append(element.price);
});

If you just want to get first element

$('#price').append(data_obj[0].price);
Void Spirit
  • 879
  • 6
  • 18
0
var my_first_json_obj = data_obj[0]; // Your first JSON obj (if it's an array of json object)
var my_price = my_first_json_obj.price; // Your price 
$('#price').append(my_price);
0

If you want only the first item's price, you don't need a loop here.

$('#price').append(data_obj[0].price);

would work here.

For further reading you can refer here

Ms.Tamil
  • 350
  • 1
  • 14
0

Following is the solution worked for my problem I use return false;

$.each(data_obj, function(index, element) {
     $('#price').append(element.price[0]);
     return false;
  });

Which gives only 1st value of array elements.

rahul.m
  • 5,572
  • 3
  • 23
  • 50