javascript - 给定相同键/值的关联数组中的连接值

标签 javascript jquery json

假设我推送到一个对象,然后对其执行 JSON.stringify

"support_advice": [{
        "text": "",
        "group": "check_lifestyle_26",
        "value": "Physical activity & Exercise"
    }]

我现在想推送一个新项目

"support_advice": [{
        "text": "",
        "group": "check_lifestyle_26",
        "value": "Overall General Health & Wellbeing"
    }]

通常情况下,我会得到以下内容

"support_advice": [{
        "text": "",
        "group": "check_lifestyle_26",
        "value": "Physical activity & Exercise"
    }, {
        "text": "",
        "group": "check_lifestyle_26",
        "value": "Overall General Health & Wellbeing"
    }]

但是,是否可以根据组连接值,以便实际输出更像

"support_advice": [{
        "text": "",
        "group": "check_lifestyle_26",
        "value": "Physical activity & Exercise, Overall General Health & Wellbeing"
    }]

完整脚本

下面的脚本所做的是循环遍历表单中的每个元素,然后创建 JSON,然后将其传递到需要的位置。

这很好用。然而,当问题出现时,就会出现复选框。系统的一部分允许您再次添加具有相同名称的表单部分。

对于文本区域或文本输入之类的东西,这很好。但是对于复选框,因为在第一个实例中可能有多个相同名称,如果我再次添加另一个包含复选框的部分,下面的脚本将无法在部分之间进行解密...

var elem = document.getElementById('rn_QuestionSubmit').elements;

            var a = [];
            var result = {};

            for(var i = 0; i < elem.length; i++)
            {
                var element = elem[i];
                var item = {};
                //item.id = element.id;
                //item.type = element.type;
                if (element.type === 'select-one') {
                    item.text = element.options[element.selectedIndex].text;
                }
                else {
                    item.text = '';
                }

                item.group = elem[i].dataset.group;

                if (element.type === 'radio' || element.type === 'checkbox') {
                    if (element.checked) {
                        item.value = element.value;
                        if (result[element.name]) { result[element.name].push(item) } else { result[element.name] = [item]};
                    }
                }
                else {
                    item.value = element.value;
                    if (result[element.name]) { result[element.name].push(item) } else { result[element.name] = [item]};
                }
            } 
            a.push(JSON.stringify(result));
            console.log('['+a.join(',') + ']');
            return('['+a.join(',') + ']');

最佳答案

您需要对代码进行一些小更改才能使复选框/单选按钮的情况发生:当 element.name 键存在于 result 中时,下一次检查将如果 result 数组中的最后一个条目具有相同的组。如果是,则修改其 value 属性,如果不是,则执行您现在已经执行的操作。

这是这部分代码:

        if (result[element.name]) {
            // Get the last entry in the array 
            var lastEntry = result[element.name].slice(-1)[0];
            // If same group, then concatenate the value instead of adding the item 
            if (lastEntry.group === item.group) {
                lastEntry.value += "," + element.value;
            } else {
                // You already had this:
                result[element.name].push(item);
            }
        // You already had this:
        } else { 
            result[element.name] = [item]
        }

关于javascript - 给定相同键/值的关联数组中的连接值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48481333/

相关文章:

javascript - 正在关注 ReactJS 教程,并想知道如何扩展它所教授的内容

javascript - Node js,使用带有 Q Promise 的 mongoose 不会调用拒绝函数

javascript - 如何将底部的灯光画廊缩略图移动到顶部?

javascript - 了解一个 JQuery 复杂链接代码示例

javascript - jQuery 子字符串或正则表达式从序列化字符串中删除多余的字符

jquery - 使用 jQuery 在 div 中显示下一个隐藏元素

javascript - 在express.js中,有没有办法同时处理ajax表单提交和浏览器帖子提交?

c# - 将 JSON 字符串解析为 DynamicJsonObject 的机器特定 (??) 行为

java - Thymeleaf 将 JSON 字符串作为 JSON 对象打印到 javascript 变量中

jquery - 在每个循环中使用 jQuery 属性从选择器开始