如何在javascript中从现有数组创建一个新数组

时间:2018-03-01 05:10:25

标签: javascript arrays json node.js

我是Javascript的新手,我必须过滤一个数组,所以我用谷歌搜索,花了我的时间找到解决方案,但没有人帮助我.PLZ帮助我。 我有以下数组,这是查询的结果:

"clause": [
    {
        "clause_id": 1,
        "clause_text": "A",
        "clause_item_id": 1,
        "item_text": "this text is related to clause 1 ",
        "item_photo": ""
    },
    {
        "clause_id": 2,
        "clause_text": "B",
        "clause_item_id": 2,
        "item_text": "this text is related to clause 2 ",
        "item_photo": ""
    },
    {
        "clause_id": 2,
        "clause_text": "B",
        "clause_item_id": 3,
        "item_text": "this text is related to clause 2",
        "item_photo": ""
    },
    {
        "clause_id": 2,
        "clause_text": "B",
        "clause_item_id": 4,
        "item_text": "this text is related to clause 2",
        "item_photo": ""
    },
    {
        "clause_id": 3,
        "clause_text": "C",
        "clause_item_id": 5,
        "item_text": "this text is related to clause 3",
        "item_photo": ""
    },
    {
        "clause_id": 3,
        "clause_text": "C",
        "clause_item_id": 6,
        "item_text": "this text is related to clause 3",
        "item_photo": ""
    },
    {
        "clause_id": 3,
        "clause_text": "C",
        "clause_item_id": 7,
        "item_text": "this text is related to clause 3",
        "item_photo": ""
    }
]

我想改变它的格式。并将其作为JSON响应发送给客户端。所以上面的数组应该通过子句id进行过滤,该子句重复多次。我想重新格式化数组,避免重复一些数组元素。所以我想要的最终数组是:

    {    "clauses": [
    {
        "cls": {
            "clause_id": 1,
            "clause_text": "A"
        },
        "items": [
            {
                "claud_item_id": 1,
                "item_text": "this text is related to clause 1",
                "item_photo": ""
            }
        ]
    },
    {
        "cls": {
            "clause_id": 2,
            "clause_text": "B"
        },
        "items": [
            {
                "claud_item_id": 2,
                "item_text": "this text is related to clause 2",
                "item_photo": ""
            },
            {
                "claud_item_id": 3,
                "item_text": "this text is related to clause 2",
                "item_photo": ""
            },
            {
                "claud_item_id": 4,
                "item_text": "this text is related to clause 2",
                "item_photo": ""
            }
        ]
    },
    {
        "cls": {
            "clause_id": 3,
            "clause_text": "C"
        },
        "items": [
            {
                "claud_item_id": 5,
                "item_text": "this text is related to clause 3",
                "item_photo": ""
            },
            {
                "claud_item_id": 6,
                "item_text": "this text is related to clause 3",
                "item_photo": ""
            },
            {
                "claud_item_id": 7,
                "item_text": "this text is related to clause 3",
                "item_photo": ""
            }
        ]
    }
]

}

PLZ帮帮我

2 个答案:

答案 0 :(得分:0)

var item_val = item_val = {"clause": [
    {
        "clause_id": 1,
        "clause_text": "A",
        "clause_item_id": 1,
        "item_text": "this text is related to clause 1 ",
        "item_photo": ""
    },
    {
        "clause_id": 2,
        "clause_text": "B",
        "clause_item_id": 2,
        "item_text": "this text is related to clause 2 ",
        "item_photo": ""
    },
    {
        "clause_id": 2,
        "clause_text": "B",
        "clause_item_id": 3,
        "item_text": "this text is related to clause 2",
        "item_photo": ""
    },
    {
        "clause_id": 2,
        "clause_text": "B",
        "clause_item_id": 4,
        "item_text": "this text is related to clause 2",
        "item_photo": ""
    },
    {
        "clause_id": 3,
        "clause_text": "C",
        "clause_item_id": 5,
        "item_text": "this text is related to clause 3",
        "item_photo": ""
    },
    {
        "clause_id": 3,
        "clause_text": "C",
        "clause_item_id": 6,
        "item_text": "this text is related to clause 3",
        "item_photo": ""
    },
    {
        "clause_id": 3,
        "clause_text": "C",
        "clause_item_id": 7,
        "item_text": "this text is related to clause 3",
        "item_photo": ""
    }
]
}
var result_val  = {};
       item_val['clause'].forEach(function(val){
                var t   = {'item_photo': val['item_photo'],'item_text':val['item_text'],'clause_item_id':val['clause_item_id']}
                if(!(val['clause_id'] in result_val))           
                        result_val[val['clause_id']]    = {}    
                if(!(val['clause_text'] in result_val[val['clause_id']]))
                        result_val[val['clause_id']][val['clause_text']] = {} 
                if('item' in result_val[val['clause_id']][val['clause_text']]){
                        result_val[val['clause_id']][val['clause_text']]['item'].push(t)
                }else{
                        result_val[val['clause_id']][val['clause_text']]['item']        = []
                        result_val[val['clause_id']][val['clause_text']]['item'].push(t)
                }
                
        })
console.log(result_val)

答案 1 :(得分:0)

所以你可以构建一个新的javascript对象,但是如果你有' items'的对象属性,它可能会很棘手。在同一个物体中重复过来。我通过使结果有一系列项目来解决这个问题。

该算法使用双循环,因此对于大型json尺寸来说它会很昂贵,但它可以工作。

这里的代码和输出假设要排序的json对象被命名为' obj'。

var newObj = {"clause":[]};
var i,j;
for(i = 0; i < obj.clause.length; i++){
  var current = obj.clause[i];
  for(j = 0; j < newObj.clause.length; j++){
    if(newObj.clause[j].cls && newObj.clause[j].cls["clause_id"] == current["clause_id"]){
      var subObj = {"claud_item_id": current["clause_item_id"], "item_text": current["item_text"], "item_photo": current["item_photo"]};    
      newObj.clause[j].items.push(subObj);
      break;
    }
  }
  if(j == newObj.clause.length){
    var subObj = {"cls": {"clause_id": current["clause_id"], "clause_text": current["clause_text"]}, 
                  "items": [{"claud_item_id": current["clause_item_id"], "item_text": current["item_text"], "item_photo": current["item_photo"]}]};     
    newObj.clause.push(subObj);
  }
}

这里是newObj的价值。

{
"clause": [{
    "cls": {
        "clause_id": 1,
        "clause_text": "A"
    },
    "items": [{
        "claud_item_id": 1,
        "item_text": "this text is related to clause 1 ",
        "item_photo": ""
    }]
}, {
    "cls": {
        "clause_id": 2,
        "clause_text": "B"
    },
    "items": [{
        "claud_item_id": 2,
        "item_text": "this text is related to clause 2 ",
        "item_photo": ""
    }, {
        "claud_item_id": 3,
        "item_text": "this text is related to clause 2",
        "item_photo": ""
    }, {
        "claud_item_id": 4,
        "item_text": "this text is related to clause 2",
        "item_photo": ""
    }]
}, {
    "cls": {
        "clause_id": 3,
        "clause_text": "C"
    },
    "items": [{
        "claud_item_id": 5,
        "item_text": "this text is related to clause 3",
        "item_photo": ""
    }, {
        "claud_item_id": 6,
        "item_text": "this text is related to clause 3",
        "item_photo": ""
    }, {
        "claud_item_id": 7,
        "item_text": "this text is related to clause 3",
        "item_photo": ""
    }]
}]
}