node.js - 比较 NodeJS 中的 JSON 对象
问题描述
你如何在 NodeJS 中读取和比较 JSON 数据?
{
"currentcount": 150,
"hasStorm": false,
"players": [
{
"world": "earth",
"armor": 0,
"name": "ceduuu",
"x": 5400.0,
"y": 115.0,
"health": 0,
"z": -11309.0,
"sort": 1,
"type": "player",
"account": "ceduuu"
},
{
"world": "-some-other-bogus-world-",
"armor": 0,
"name": "Egga_",
"x": 0.0,
"y": 64.0,
"health": 0,
"z": 0.0,
"sort": 1,
"type": "player",
"account": "Egga_"
},
有 150 个个人数据,我想暂时将 X、Z 和帐户存储在一个变量中,以便我进行比较。但是,我将如何检查第一个玩家的数据以及如何检查第二个玩家的数据?
解决方案
目前尚不清楚您要将对象与什么进行比较,但这里有一些代码可以帮助您入门。
const comparePlayers = (players) => {
// iterates over each object
players.forEach(player => {
// destructure object
const { x, z, account } = player;
// your compare players logic
});
};
comparePlayers(jsonData.players);
推荐阅读
- xpages-ssjs - 我正在使用带有计算值的复选框组(垂直布局)。我的页面的显示不是用户友好的,因为所有 20 个值都是垂直对齐的
- angular - 打字稿自定义装饰器:this的上下文
- sql - 如何更改 SQL 中的 iOS 单引号?
- c++ - constructors,inheritance,stack,heap,this-pointer and segfaults in c++
- laravel - 如何使用 .htaccess 将公共 URL 重定向到非公共 URL?
- javascript - 我应该将选择菜单的设置包装到承诺中吗?
- laravel - 当我尝试通过 Insomnia REST API 访问我的路由时,Laravel Passport 抛出 MethodNotAllowedHttpException 异常
- regex - 正则表达式长度约束
- git - git合并相关但不同的文件
- c# - 损坏的集合名称和属性名称 c# Mongodb