繁体   English   中英

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

[英]how to create a new array from existing array in javascript

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

"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": ""
            }
        ]
    }
]

}

LZ帮帮我

 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)

因此,您可以构建一个新的 javascript 对象,但如果您在同一对象中重复了“项目”的对象属性,则可能会很棘手。 我通过使结果具有像这样的项目数组来解决这个问题。

该算法使用双 for 循环,因此对于大的 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": ""
    }]
}]
}

暂无
暂无

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

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