javascript - 使用 jQuery 从 JSON 文件中提取数据并将其分成组

标签 javascript jquery json ajax custom-data-attribute

我目前正绞尽脑汁让网站的某一特定部分正常工作。

它包括单击按钮并检查其“数据组”属性。这应该打开另一个 div 并用从本地 JSON 文件中提取的内容填充它,我必须根据每个按钮的数据组属性对其进行过滤。

我当前的 JSON 如下所示:

 [
{
    "group": "editing",
    "question": "How does Editing work?",
    "answer": "Editing Editing Editing Editing Editing Editing works just fine is pretty cool to see it working hey this is just placeholder text to check whether it is working or not."
},
{
    "group": "distribution",
    "question": "How does Distribution work?",
    "answer": "Distribution Distribution Distribution Distribution Distribution Distribution works just fine is pretty cool to see it working hey this is just placeholder text to check whether it is working or not."
},
{
    "group": "setup",
    "question": "How does Setup work?",
    "answer": "Setup Setup Setup Setup Setup Setup Setup works just fine is pretty cool to see it working hey this is just placeholder text to check whether it is working or not."
},
{
    "group": "profiles",
    "question": "How do Profiles work?",
    "answer": "Profiles Profiles Profiles Profiles Profiles Profiles Profiles Profiles works just fine is pretty cool to see it working hey this is just placeholder text to check whether it is working or not."
},
{
    "group": "payment",
    "question": "How does Payment work?",
    "answer": "Payment Payment Payment Payment Payment Payment Payment Payment Payment works just fine is pretty cool to see it working hey this is just placeholder text to check whether it is working or not."
}
{
    "group": "about",
    "question": "How does Payment work?",
    "answer": "Payment Payment Payment Payment Payment Payment Payment Payment Payment works just fine is pretty cool to see it working hey this is just placeholder text to check whether it is working or not."
}

]

我的 JavaScript 看起来像这样:

$('.groupBtn').on('click', function(data){
    data.preventDefault();

    var $root = $('html, body');
    $root.animate({
        scrollTop: $('.angle').offset().top
    }, 500);

    var attributeId = $(this).data('group');

    if ($(this).attr('group') == attributeId) {

    } else {
        $(this).siblings().removeClass('selected');
        $(this).addClass('selected');

        $.getJSON("js/faq-content.json", function() {


        })
        .done(function(data){

            $.each(data.questions, function(i, question){
                console.log(question);
                $('.resultsList.open').append('<article class="result"><div class="question"><p>'+ question.question +'</p><div class="plus"></div></div>');
            });
        });
    }

    $('.resultsList').each(function(){

        $(this).hide();
        var selectedAttribute = $('.selected').data('group');

        if ($(this).data('group') == selectedAttribute) {
            $(this).fadeIn(200);
        };
    });
});

最佳答案

$.getJSON("js/faq-content.json", function() {

    })
    .done(function(data){
      var groupQuestions = data.questions.filter(data => data.group == attributeId);
        $.each(groupQuestions, function(i, question){
            console.log(question);
            $('.resultsList.open').append('<article class="result"><div class="question"><p>'+ question.question +'</p><div class="plus"></div></div>');
        });
    });

关于javascript - 使用 jQuery 从 JSON 文件中提取数据并将其分成组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43992532/

相关文章:

c# - 将列表序列化为 JSON

javascript - Node 休息客户端获取局部变量

php - JavaScript mysql 接口(interface)?

javascript - MS CRM 2013为脚本类型的WebResources添加版本号

javascript - 使用 .html() 更新渲染 MathJax

javascript - 获取 17-05-2012 格式的日期

javascript - 使用 Gulp 将 jsx 转换为 js 时出现 React 语法错误

Javascript regEx 电子邮件仅限 3 个域

java - 从Map中获取键值对

java - Android:将 httpURLConnection 连接到服务器