gpt4 book ai didi

javascript - Node.js 从数组中访问元素

转载 作者:行者123 更新时间:2023-11-28 18:39:53 24 4
gpt4 key购买 nike

我正在向 http://localhost:3000 的本地服务器执行获取请求.

我正在通过 Node.JS 命令行处理 request.body (JSON 数据数组)。

我只想获取名称元素,而不是其他两个元素。我尝试提供索引,但没有成功。

http://localhost:3000
[ { name: 'Ankit', drag: true, jqyoui_pos: 0 },
{ name: 'Nikhil', drag: true, jqyoui_pos: 3 },
{ name: 'Ajay', drag: true, jqyoui_pos: 4 },
{ name: 'Vineet', drag: true, jqyoui_pos: 5 } ]

最佳答案

您可以使用 map 功能过滤掉名称。见下文。

    var obj = [
{name: 'Ankit', drag: true, jqyoui_pos: 0},
{name: 'Nikhil', drag: true, jqyoui_pos: 3},
{name: 'Ajay', drag: true, jqyoui_pos: 4},
{name: 'Vineet', drag: true, jqyoui_pos: 5}
];


var result = obj.map(function(a) {return a.name;});

console.log(result);

关于javascript - Node.js 从数组中访问元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36299708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com