如何在javascript中从现有数组创建一个新数组
Posted
技术标签:
【中文标题】如何在javascript中从现有数组创建一个新数组【英文标题】:how to create a new array from existing array in javascript 【发布时间】:2018-08-09 03:00:26 【问题描述】:我是 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": ""
]
]
请帮帮我
【问题讨论】:
标题提到了过滤——误导,你想转换数据——无论哪种情况,SO都不是代码编写服务——你试过什么 请使用谷歌获得初步帮助。然后尝试实现一些东西,如果你被困在某个地方或者你想要替代品,请分享你的尝试,我们会帮助你 顺便说一句,你需要的格式是不可能你不能在一个对象中有多个同名的键......你可能想要items
成为一个对象数组
【参考方案1】:
因此,您可以构建一个新的 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": ""
]
]
【讨论】:
谢谢,它对我有用,这正是我想要的。 @约翰【参考方案2】: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)
【讨论】:
无法在 Object 中使用相同的多个键(因此您的例外不可能)。您可以使用这种替代解决方案以上是关于如何在javascript中从现有数组创建一个新数组的主要内容,如果未能解决你的问题,请参考以下文章
如何在不创建新数组的情况下用另一个数组扩展现有 JavaScript 数组
在 JavaScript 中从 1..20 创建整数数组的最简单方法