第一个:{ p: "category", category1: "001", name1: "100", category2: "001", name2: "100", category3: "001", name3: "100", category12: "001", name12: "100",}第二个value对应上面的nameX{ p: "category", category: [ { value: "001", name: "100" }, { value: "001", name: "100" }, { value: "001", name: "100" }, { value: "001", name: "100" } ]}如何将第一个转换成第二个?
1 回答
Cats萌萌
TA贡献1805条经验 获得超9个赞
var obj = {p: "category",category : []};
var src = {
p: "category",
category1: "001",
name1: "100",
category2: "001",
name2: "100",
category3: "001",
name3: "100",
category12: "001",
name12: "100",
};
for(var key in src){
if(/category/.test(key)){
obj.category.push({
value : src[key],
name : src['name' + key.match(/\d+/)[0]]
});
}
}
添加回答
举报
0/150
提交
取消