简体   繁体   English

创建一个二维 object 并在 javascript 中的 json 中打印

[英]create a Bidimensional object and print it in json in javascript

I have some problem creating an object array 2d with javascript (no jquery)我在使用 javascript(无 jquery)创建 object 数组 2d 时遇到一些问题

the output should be some one of this: output 应该是其中之一:

{"Switch":[{"name":"Switch 1","description":"Switch 1"},{"name":"Switch 2","description":"Switch 2"},{"name":"Switch 3","description":"Switch 3"},{"name":"Switch 4","description":"Switch 4"},{"name":"Switch 5","description":"Switch 5"},{"name":"Switch 6","description":"Switch 6"},{"name":"Switch 7","description":"Switch 7"}]}

the only one goal I reach is using two different arrays, one for the name and one for the description, doing someting like this:我达到的唯一一个目标是使用两个不同的 arrays,一个用于名称,一个用于描述,执行如下操作:

var swName = [];
var swDesc = [];
var i;
var namePrefix = "swtchname";
var descPrefix = "swtchdesc";
var count = document.querySelectorAll('[id^="swtchname"]').length; //if exist swtchname exist olso description

for(i=0;i<count;i++){
    var name;
    var desc;
    name = document.getElementById(namePrefix + i).value;
    desc = document.getElementById(descPrefix + i).value;
    swName.push(name);
    swDesc.push(desc);
}

I would like to create an object with two-dimension and put all the data in it.我想创建一个二维的 object 并将所有数据放入其中。

How can I change this?我怎样才能改变这个? cause if I create an object like this, it should be easy also print the JSON format, right?因为如果我像这样创建一个 object,它应该也很容易打印 JSON 格式,对吧?

Assuming you have <input> elements with swtchname{x} and swtchdesc{x} as id where x is a number, try something like:假设您有swtchname{x}swtchdesc{x}作为id<input>元素,其中x是一个数字,请尝试以下操作:

 var namePrefix = "swtchname"; var descPrefix = "swtchdesc"; var names = document.querySelectorAll('[id^="swtchname"]'); var X = { Switch: [] }; names.forEach(function(i) { var desc = document.getElementById(i.id.replace(namePrefix, descPrefix)); X.Switch.push({ name: i.value, desc: (desc && desc.value) || '' }); }); console.log(X);
 <input id="swtchname1" value="name 1"> <input id="swtchdesc1" value="desc 1"> <input id="swtchname2" value="name 2"> <input id="swtchdesc2" value="desc 2"> <input id="swtchname3" value="name 3"> <input id="swtchdesc3" value="desc 3"> <input id="swtchname4" value="name 4"> <input id="swtchdesc4" value="desc 4"> <input id="swtchname5" value="name 5"> <input id="swtchdesc5" value="desc 5"> <input id="swtchname6" value="name 6"> <input id="swtchdesc6" value="desc 6">

Yeah after a lot of try I found my way:!是的,经过多次尝试,我找到了自己的方法:! I did a thing like this:我做了这样的事情:

var SWarr = [];
    for (var i = 0; i < count; i++) {
        SWarr.push({
            name: document.getElementById(namePrefix + i).value,
            description: document.getElementById(descPrefix + i).value,
        });
    }

    var jsonFormInfo = (JSON.stringify({ Switch: [SWarr] }));

Thank a lot for the input!非常感谢您的意见!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM