JS将各值拼接JSON串

Keep Open and Learning
Post Reply
jiataifeng
Posts: 136
Joined: 30 Apr 2009 06:31

JS将各值拼接JSON串

Post by jiataifeng » 16 Nov 2018 03:50

[
{
"Test1": "Example Data",
"Test2": 42,
"Test3": true,
"Test4": "Editable Data",
"Test5": "Editable Data copy"
}
]

拼接代码

<script>

var params = [];
//for(var i = 0; i < 3; i++){
params.push({"Test1": "Example Data","Test2":42,"Test3":true,"Test4": "Editable Data","Test5": "Editable Data copy"});
//}

var json = JSON.stringify(params);
alert(json);
</script>

jiataifeng
Posts: 136
Joined: 30 Apr 2009 06:31

Re: JS将各值拼接JSON串

Post by jiataifeng » 16 Nov 2018 05:54

判断数组的维度

Code: Select all

<script type="text/javascript">
	var arr = [1,2,3,[1,2,3,1,3,[1,2,3,6,4,[1,2,3,1]]],2],a=1;
	function multiarr(arr){
		for (i=0;i<arr.length;i++){
			if(arr[i] instanceof Array){
				a++;
				arr = arr[i];
				multiarr(arr);
			}
		}
		return a;
	}
	console.log(multiarr(arr));
</script>

Post Reply