javascript - 检查 JSON 文件内数组中的值

标签 javascript jquery json

我有一个 JSON 文件,我为其创建了一个 jQuery 函数来查找匹配值并将其显示在 div 中。我不太确定如何比较 JSON 文件中的 activities 数组中的单个值。它似乎只返回整个数组。

如何检查 JSON 文件中的每个 resort 并查看 JSON 文件内的 activities 数组中的值之一是否包含特定值,例如 水肺潜水

JavaScript:

var destination = $('option:selected', "#destination").attr('value');
var comfortLevel = $('option:selected', "#comfortLevel").attr('value');
var activities = $('option:selected', "#activities").attr('value');
var date = $('option:selected', "#date").attr('value');
var price = $('option:selected', "#price").attr('value');

$.getJSON('resort.json', function(data) {
    $.each(data.resorts, function(key, val) {
        if (destination == val.destination) {
            if (comfortLevel == val.comfortLevel || activties == val.activities || date == val.startDate || price > val.price) {
                $("#resortData").html("<img src= "+val.picture+" class='miniPic'> Destination: "+ val.destination + "<br>" + "Name: " + val.name + "<br>" +"Location: " 
                + val.location + "<br>" + "Comfort: " + val.comfortLevel + " Star <br>" + "Activities: " + val.activities + "<br>" + "Price: £" 
                + val.price + "<br>" + "Start Date: " + val.startDate + "<br>" + "End Date: " + val.endDate + "<br>" + "Description: " + val.short_description 
                + "<br><br>" + "<a href=':" + val.url +"'>Click HERE for more info</a>");
            }
        }
    });
});

度假村.JSON:

{ 
    "resorts": [
        {
            "id":"resort1",
            "destination":"Carribean",
            "name":"Les Boucaniers",
            "location":"Martinique",
            "comfortLevel": "4",
            "activities":["water skiing", "tennis", "scuba diving", "kitesurf", "spa"],
            "price":1254,
            "startDate":"2016-01-01",
            "endDate":"2016-12-31",
            "short_description":"The resort of Les Boucaniers is located on the laid-back beach-covered south coast of the island, and is perfectly placed for Martinique holidays that are both relaxing and awe-inspiring.",
            "picture":"images/resort1pic1small.jpg",
            "long_description":"A divers' paradise in the Baie du Marin, a legendary spot.<br>Its bungalows are discreetly lodged in a tropical garden beside the white sand beach in superb Marin Bay. A magical site where you can enjoy a taste of everything, alone or with family or friends. Try water sports and the magnificent Club Med Spa*. You'll be enchanted by the exotic flavours of the local cuisine and the joyful spirit of the Caribbean.",
            "url":"resorts/resort1.html"
        },
        {
            "id":"resort2",
            "destination":"Indian Ocean",
            "name":"La Plantation d'Albion",
            "location":"Mauritius",
            "comfortLevel": "5",
            "activities":["kids club","golf", "scuba diving", "flying trapeze", "tennis", "sailing", "spa"],
            "price":2062,
            "startDate":"2016-01-01",
            "endDate":"2016-12-31",
            "short_description":"Beautifully located in one of the last remote creeks on the island, La Plantation d'Albion Club Med welcomes the most demanding of guests into a world of supreme refinement.",
            "picture":"images/resort2pic1small.jpg",
            "long_description":"In a remote beauty spot, savour the luxury of Mauritian lifestyle. <br> The idyllic natural setting is enhanced by the sublime decor designed by Marc Hertrich and Nicolas Adnet, and the Resort's top-end comfort is perfectly reflected in its beautifully spacious rooms. The exceptional CINQ MONDES Spa* and luxurious overflow pool add an ideally Zen touch.<br> The Resort is entirely devoted to fulfilling its guests' desires and offers discreet, personal service in its swimming areas, bars and 'Table Gourmet' restaurants.",
            "url":"resorts/resort2.html"
        }
]}

最佳答案

How do I check through each resortin the JSON file and see if one of the values in the activities array inside the JSON file contains a specific value like scuba diving

在事件数组上使用indexOf()

注意这里有一个拼写错误(activties):if (comfortLevel == val.comfortLevel || activties == val.activities ...

关于javascript - 检查 JSON 文件内数组中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34641847/

相关文章:

javascript - 推送器未正确发布到服务器

java - 使用 Java 有条件地访问同一级别的 JsonElements

javascript - 如何解决 Three.js 游戏中随着时间的推移出现的滞后问题

javascript - Highcharts 饼图数据损坏

php - 跟踪横幅 View

javascript - 限制字段​​的函数在 mozilla 和 I.E 中不起作用

jQuery UI 可排序框始终具有高度 : 0

c# - 如何通过iframe获取另一个页面的asp隐藏字段值

c# - ASP.NET MVC4 Web API MediaTypeFormatter Converter 将 XElement 转换为 JSON

javascript - 自动提交表单