最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get only 1st element of JSON data? - Stack Overflow

programmeradmin0浏览0评论

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'

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'

Share Improve this question edited Nov 12, 2019 at 13:28 rahul.m asked Sep 28, 2018 at 12:41 rahul.mrahul.m 5,8543 gold badges29 silver badges55 bronze badges 2
  • Possible duplicate of How to access first element of JSON object array? – Anuga Commented Sep 28, 2018 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 Commented Sep 28, 2018 at 12:46
Add a comment  | 

8 Answers 8

Reset to default 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>

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"]);

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

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,

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);
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);

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

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.

发布评论

评论列表(0)

  1. 暂无评论