现在有一个json 需要存入 数组 jsonarry 怎样弄?{
"data": [
{
"typeid": 2,
"txt": "人力资源",
"title": "",
"children": [
{
"nameid": 6,
"txt": "入职手续",
"code": "crylbx",
"title": "身份证、正反面等入职"
},{
"nameid": 7,
"txt": "社保接入",
"code": "crylbx",
"title": "社保卡号、社保密码社保接入"
}
]
}, {
"typeid": 1,
"txt": "计划生育",
"title": "",
"children": [
{
"nameid": 1,
"txt": "准生证",
"code": "zsz",
"title": "准生证准备材料"
}, {
"nameid": 2,
"txt": "独生子女证",
"code": "dszn",
"title": "独生子女证 准备材料"
}, {
"nameid": 3,
"txt": "计划生育证",
"code": "jhsy",
"title": "计划生育证 准备材料\r\n1、材料1\r\n2、材料3\r\n3、材料3"
}
]
}
]
}
上边是 json
下边这个怎么写 或者 哪位大神 有更好的方法
var jsonarry=[ ];
$.each(data.data, function(i, item) {
jsonarry.push({
//将json 追加入数组
name: this.name,
txt: this.txt,
children: 这里怎么写
});
});
"data": [
{
"typeid": 2,
"txt": "人力资源",
"title": "",
"children": [
{
"nameid": 6,
"txt": "入职手续",
"code": "crylbx",
"title": "身份证、正反面等入职"
},{
"nameid": 7,
"txt": "社保接入",
"code": "crylbx",
"title": "社保卡号、社保密码社保接入"
}
]
}, {
"typeid": 1,
"txt": "计划生育",
"title": "",
"children": [
{
"nameid": 1,
"txt": "准生证",
"code": "zsz",
"title": "准生证准备材料"
}, {
"nameid": 2,
"txt": "独生子女证",
"code": "dszn",
"title": "独生子女证 准备材料"
}, {
"nameid": 3,
"txt": "计划生育证",
"code": "jhsy",
"title": "计划生育证 准备材料\r\n1、材料1\r\n2、材料3\r\n3、材料3"
}
]
}
]
}
上边是 json
下边这个怎么写 或者 哪位大神 有更好的方法
var jsonarry=[ ];
$.each(data.data, function(i, item) {
jsonarry.push({
//将json 追加入数组
name: this.name,
txt: this.txt,
children: 这里怎么写
});
});
解决方案 »
- 三天新手问一个非常简单奇怪的问题...,满分全送!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- JS 如何判断 checkboxlist 没被选中(勾选)
- ?????【位非 ~ 】??????
- 发帖认识下
- javascript创建的textarea在表单提交时不上传,而其它的input text可以
- 求一简单正则表达式
- 新手问题1:如何扩展对象类型
- 能不能对一个字符串进行按字节取值?
- 请教如何用javascript和ccs式样表编写xp风格的网页?还有我用同样的方法要写一个带金属过渡塞色的表格怎样解决?最好原码?问题解决了还
- 怎样自动刷新页面?
- disabled 简单问题,戳进来指导一下
- .getElementsByClassName()return出来是一个对象不支持.style属性吗?
let jsonData = {
"data": [
{
"typeid": 2,
"txt": "人力资源",
"title": "",
"children": [
{
"nameid": 6,
"txt": "入职手续",
"code": "crylbx",
"title": "身份证、正反面等入职"
},{
"nameid": 7,
"txt": "社保接入",
"code": "crylbx",
"title": "社保卡号、社保密码社保接入"
}
]
},
{
"typeid": 1,
"txt": "计划生育",
"title": "",
"children": [
{
"nameid": 1,
"txt": "准生证",
"code": "zsz",
"title": "准生证准备材料"
}, {
"nameid": 2,
"txt": "独生子女证",
"code": "dszn",
"title": "独生子女证 准备材料"
}, {
"nameid": 3,
"txt": "计划生育证",
"code": "jhsy",
"title": "计划生育证 准备材料\r\n1、材料1\r\n2、材料3\r\n3、材料3"
}
]
}
]
};
let jsonArr=[ ];
for(let i in jsonData){
jsonArr.push({
i: jsonData[i]
})
}
console.log(jsonArr);
<script>
var jsonarry = new Array();
var cArray = new Array();
cArray.push(new child("me", "test", []));
cArray.push(new child("you", "test2", []));jsonarry.push({
name: "",
txt: "",
children: cArray
});alert(jsonarry[0].children[0].name);
alert(jsonarry[0].children[1].name);function child(name, txt, children){
this.name = name;
this.txt = txt;
this.children = children;
}
</script>
let arr = Array.from(json)搞定
"data": [
{
"typeid": 2,
"txt": "人力资源",
"title": "",
"children": [
{
"nameid": 6,
"txt": "入职手续",
"code": "crylbx",
"title": "身份证、正反面等入职"
},{
"nameid": 7,
"txt": "社保接入",
"code": "crylbx",
"title": "社保卡号、社保密码社保接入"
}
]
},
{
"typeid": 1,
"txt": "计划生育",
"title": "",
"children": [
{
"nameid": 1,
"txt": "准生证",
"code": "zsz",
"title": "准生证准备材料"
}, {
"nameid": 2,
"txt": "独生子女证",
"code": "dszn",
"title": "独生子女证 准备材料"
}, {
"nameid": 3,
"txt": "计划生育证",
"code": "jhsy",
"title": "计划生育证 准备材料\r\n1、材料1\r\n2、材料3\r\n3、材料3"
}
]
}
]
};
var jsonArr=[ ];
for(var i in jsonData.data){
jsonArr.push({
typeid: jsonData.data[i].typeid,//json里面没有name,故写了typeid
txt: jsonData.data[i].txt,
children: jsonData.data[i].children
});}
console.log(jsonArr);
const arr = data.map(el => el)
console.log(arr)结果[ { typeid: 2,
txt: '人力资源',
title: '',
children: [ [Object], [Object] ] },
{ typeid: 1,
txt: '计划生育',
title: '',
children: [ [Object], [Object], [Object] ] } ]