从另一个JSON对象创建JSON对象 [英] create JSON object from another JSON object

查看:89
本文介绍了从另一个JSON对象创建JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道标题是否适合我想做的事情.

I don't know if the title is the right one for what I want to do.

我有以下对象数组

    array([0]: {category: "10",
                question: "101"},
          [1]: {category: "10",
                question: "102"},
          [2]: {category: "20",
                question: "201"}
    );

我想将所有元素分组为这样的东西:

And I want to group all the elements into something like this:

    array([0]: {category: "10", {question: "101",
                                 question: "102"}},
                category: "20", {question: "201"}});

我不知道这是否可行,或者是否还有其他更好的方法来解决这个问题(也许是二维数组?),但是任何帮助将不胜感激.

I don't know if this is possible or if there's another better way to solve this problem (maybe with a two-dimensional array?) but any help would be very appreciated.

谢谢!

很抱歉缺少信息,这是我创建对象数组的方式:

Sorry for the lack of information, here is how I create my array of objects:

    var arr_sections = [];
    var arr_questions = [];
var jsonObj = [];
$('.ui-sortable-nostyle').each(function(){
    arr_sections.push($(this).attr("sec_id"));              
});

// create json object with pairs category - question 
$(".sortable2").each(function(i){                           
    $(this).children().each(function(j){    
        var cat = arr_sections[i];
        jsonObj.push({
            category: arr_sections[i],
            question: $(this).attr("ques_id")
        });     
    });
});  

并得到具有以下结构的对象数组:

And got an array of objects with the following structure:

    [Object, Object, Object, Object, Object]
    0: Object
       category: "1052"
       question: "3701"
       __proto__: Object 
    1: Object
       category: "1053"
       question: "3702"
       __proto__: Object
    2: Object
       category: "483"
       question: "1550"
       __proto__: Object
    3: Object
       category: "483"
       question: "1548"
       __proto__: Object
    4: Object
       category: "483"
       question: "1549"
       __proto__: Object
    length: 5

试图创建我的新数组:

    for(var i = 0; i < jsonObj.length; i++){
        temp[jsonObj[i].category] = jsonObj[i].question;
}

但是我没有得到所有值:

But I don't get all values:

    [483: "1549", 1052: "3701", 1053: "3702"] 

推荐答案

您有:

var arr = [ 
  {category: "10", question: "101"},
  {category: "10", question: "102"},  
  {category: "20", question: "201"}
];

您想要(我想):

var arrRequired = [
  {category: "10", question : ["101", "102"] },
  {category: "20", question : ["201"] }
];

我会给你的

var obj = {
  "10" : ["101", "102"],
  "20" : ["201"]
};

要将您拥有的东西转换成我将给您的东西,您可以执行以下操作:

To convert what you have to what i will give you, you can do:

var result = {};
arr.forEach(function(item) {
  if (!result[item.category]) 
    result[item.category] = [item.question];
  else
    result[item.category].push(item.question);
});

要从我给你的东西中得到想要的东西,你可以做:

To get to what you want from what i gave you, you can do:

var arrRequired = [];
for (var category in result){
  arrRequired.push({ 'category' : category, 'question' : result[category] });
}

如果那不是您想要的,那么也许您应该使用有效的语法对其进行更好的解释.

If that is not what you want, then perhaps you should explain it better, using valid syntax.

这篇关于从另一个JSON对象创建JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆