javascript - 比较 NodeJs 和 Express 中的两个 JSON

标签 javascript node.js json express

我正在尝试比较 NodeJs 中的两个 JSON 文件。 如果有与地点 id 匹配的评论,我需要将评论数据推送到地点 JSON 中。如果没有匹配的评论,则会推送一个空数组。

这是代码:

//放置 JSON

[{
        "id": 1,
        "title": "Hotel in Sidney",
        "maxNumberOfGuests": 5,
        "description": "Quiet place by the water.",
        "createdAt": "2019/12/7 14:34",
        "price": 120
    },
    {
        "id": 2,
        "title": "Cabin in Italy",
        "maxNumberOfGuests": 2,
        "description": "Romantic lake cabin for two.",
        "createdAt": "2019/4/7 10:00",
        "price": 250
    }
]

//评论 JSON

[{
        "id": 1,
        "numberOfStars": 3,
        "content": "Comfy place",
        "placeId": 1,
        "createdAt": 12345
    },
    {
        "id": 2,
        "numberOfStars": 4,
        "content": "Awesome lake view.",
        "placeId": "",
        "createdAt": 23456
    }
]

这是期望的结果:

[{
        "id": 1,
        "title": "Hotel in Sidney",
        "maxNumberOfGuests": 5,
        "description": "Quiet place by the water.",
        "createdAt": "2019/12/7 14:34",
        "reviews": [{
            "id": 1,
            "numberOfStars": 3,
            "content": "Comfy place",
            "placeId": 1,
            "createdAt": 12345
        }],
        "price": 120
    },
    {
        "id": 2,
        "title": "Cabin in Italy",
        "maxNumberOfGuests": 2,
        "description": "Romantic lake cabin for two.",
        "createdAt": "2019/4/7 10:00",
        "reviews": [],
        "price": 250
    }
]

这是我能走多远:

places.forEach(p => {
  const { id } = p;
  console.log(id);
  return id;
});

reviews.forEach(r => {
  const { id, numberOfStars, content, placeId, createdAt } = r;
  // console.log(id, numberOfStars, content, placeId, createdAt);
  console.log(r);
  return r;
});

//node express routes to places where will display the desired result. 

router.get('/places', function(req, res) {
  res.json(places);
});

我就是无法让它发挥作用,需要一些帮助。

提前致谢。

最佳答案

试试这个

let places =[{
        "id": 1,
        "title": "Hotel in Sidney",
        "maxNumberOfGuests": 5,
        "description": "Quiet place by the water.",
        "createdAt": "2019/12/7 14:34",
        "price": 120
    },
    {
        "id": 2,
        "title": "Cabin in Italy",
        "maxNumberOfGuests": 2,
        "description": "Romantic lake cabin for two.",
        "createdAt": "2019/4/7 10:00",
        "price": 250
    }
];

let reviews =[{
        "id": 1,
        "numberOfStars": 3,
        "content": "Comfy place",
        "placeId": 1,
        "createdAt": 12345
    },
    {
        "id": 2,
        "numberOfStars": 4,
        "content": "Awesome lake view.",
        "placeId": "",
        "createdAt": 23456
    }
];
places.forEach(function(place) {
  place.reviews = reviews.filter(review => review.placeId ===place.id);
});


console.log(places);

关于javascript - 比较 NodeJs 和 Express 中的两个 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59794958/

相关文章:

node.js - Express redis session 存储停止在新机器上工作

javascript - AngularJS 将变量传递到使用 $http + $interval 的服务中

javascript - AngularJS 通过属性将对象分配给 ng 模型

javascript - 从 _.map() 返回唯一值

javascript - 如何检测 textContent 何时溢出其父级

node.js - Express 不会在虚拟主机中提供静态 ejs 文件

c# - 如何获取JSON字符串中的元素

javascript - 在现有暂存器上创建一个新暂存器

javascript - 在 Laravel 中使用 Ajax 请求进行搜索

mysql - 如何从对象动态设置mysql查询中的值?