本文介绍了node.js表单元素数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在NodeJS中解析表单元素数组?

How to parse form elements array in NodeJS ?

类似于PHP,对于名称为answers[0], answers[1], answer[2] .... answers[n]

Like in PHP we get $_POST['answers'] for form elements with names: answers[0], answers[1], answer[2] .... answers[n]

推荐答案

为此,我有自己的解决方案,例如,我正在var obj中获取数据:

I got my own solution for this, for example I am getting data in var obj:

console.log("\n\n\n\n\n\n\n\n\n");

var obj = { 
    'answer[\'test\']': 'first',
    'answer[\'2\']': 'second' 
};

var new_obj = {};
for(key in obj){
    key.replace(/([^\[]+)\['([^\]]+)'\]/g, function($0, $1, $2){
            new_obj[$1] = new_obj[$1] || {};
        new_obj[$1][$2] = obj[key];
    })
}

console.log(new_obj);

这篇关于node.js表单元素数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 11:54