Javascript 如何从没有键的JSON对象中获取值

Javascript 如何从没有键的JSON对象中获取值,javascript,json,response,key-value,Javascript,Json,Response,Key Value,我发出http请求,然后从SQL表中获取值 router.get('/', function(req, res, next) { controller.getAllPosts( function(err,posts){ if(err){ res.status(500); res.end(); }else{ res.json(posts); } 我得到的回应是这样

我发出http请求,然后从SQL表中获取值

router.get('/', function(req, res, next) {
    controller.getAllPosts( function(err,posts){
        if(err){
            res.status(500);
            res.end();
        }else{           
            res.json(posts);
}
我得到的回应是这样的:

[
  {
    "id_post": 1,
    "description": "Hola",
    "username": "jumavipe",
    "image": "1.jpg"
  },
  {
    "id_post": 2,
    "description": "no se",
    "username": "jacksonjao",
    "image": "2.jpg"
  },
  {
    "id_post": 3,
    "description": "nuevo tatuaje de bla bla bla",
    "username": "jumavipe",
    "image": "3.jpg"
  }
]
var description = posts.getJSONObject("LabelData").getString("description");
如何仅从第3篇文章中获得描述

我做不到:

var desc= posts[2].description
我在网上查了一下,我试过这样的方法:

[
  {
    "id_post": 1,
    "description": "Hola",
    "username": "jumavipe",
    "image": "1.jpg"
  },
  {
    "id_post": 2,
    "description": "no se",
    "username": "jacksonjao",
    "image": "2.jpg"
  },
  {
    "id_post": 3,
    "description": "nuevo tatuaje de bla bla bla",
    "username": "jumavipe",
    "image": "3.jpg"
  }
]
var description = posts.getJSONObject("LabelData").getString("description");
如果我的json数组没有键,我应该在getJSONObject中使用什么作为参数

我找不到有效的东西。如何从json数组中的一个对象获取该值?

使用array.prototype.find 如果没有任何浏览器兼容性问题,可以使用Array.prototype.find

使用Array.prototype.filter Array.prototype.filter几乎在大多数浏览器中都受支持,可以正常工作

var selected_posts = posts.filter(function(item) {
  return item.id_post == 3;
});

console.log(selected_posts[0].description);

索引从0开始,因此您需要posts[2]。而不是description。请注意,这与JSON无关。当您试图访问该值时,您正在访问一个对象数组,而不是字符串。JSON是一种文本表示法。如果你在处理字符串,你只能在JavaScript中处理JSON。爪哇!=JavaScript。你的意思是说我如何仅从id_post为3的帖子中获得描述?相关:谢谢@JulianaVillegas,如果它起作用,请将其标记为选中: