简体   繁体   English

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

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

I am a newbie in Javascript, I have to filter an array so I have googled, and spent my time to find the solution but no one helped me.PLZ help me.我是 Javascript 的新手,我必须过滤一个数组,所以我用谷歌搜索,并花时间寻找解决方案,但没有人帮助我。请帮助我。 I have the following array which is the result of a query:我有以下数组,它是查询的结果:

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

I want to change it's format.我想改变它的格式。 and send it as JSON response to the client.并将其作为 JSON 响应发送给客户端。 so the above array should be filtered by clause id which is repeated many times.所以上面的数组应该通过重复多次的子句 id 过滤。 I want to reformat the array and avoid from the repetition of some array elements.我想重新格式化数组并避免重复某些数组元素。 so the final array which I want is this:所以我想要的最终数组是这样的:

    {    "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 help me 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)

So you can build out a new javascript object, but it might be tricky if you have object attributes of 'items' repeated over in the same object.因此,您可以构建一个新的 javascript 对象,但如果您在同一对象中重复了“项目”的对象属性,则可能会很棘手。 I fixed this by making the results have an array of items like so.我通过使结果具有像这样的项目数组来解决这个问题。

The algorithm is using a double for loop so it would be expensive for large json sizes, but it works.该算法使用双 for 循环,因此对于大的 json 大小来说会很昂贵,但它有效。

Here's the code and output assuming the json object to be sorted is named 'obj'.这是假设要排序的 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);
  }
}

Here's the value of newObj.这是 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.

相关问题 通过重复原始数组从现有数组 JavaScript 创建新数组 - Create new array from existing array JavaScript by repeating the original array 使用javascript / jquery从现有数组创建一个新数组,该数组保存一个项目存在的次数 - Create a new array from an existing array that holds the count for how many times an item exist using javascript/jquery 如何从具有不同结构的现有数组创建新数组 - How to create new array from existing array with different structure 如何使用 TypeScript 中现有数组中的选定元素创建新数组? - How to Create a new array with selected element from existing array in TypeScript? 如何从现有的对象数组中打印一个新数组 javascript - How to print a new array from existing array of object javascript 从两个现有的 arrays 按 id 过滤 Javascript 创建一个新数组 - Create a new array from two existing arrays filtering by id with Javascript JavaScript 通过过滤 object 值从现有创建新数组 - JavaScript create new Array from existing by filtering object value 根据现有的嵌套值创建新数组-Javascript - Create new array from existing nested values - Javascript 从现有数组属性创建新数组 - Create new array from an existing array attributes 如何在javascript中从现有数组创建对象数组? - How to create an array of objects from an existing array in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM