㈠ js如何将 一组数字 匹配到json数组里,判断是否存在,并获得json数组的值
varparam=[{id:1,value:[1,3,5]},{id:1,value:[1,2,3]}];
varcheck=[1,3,5];
varjudge=function(check,param){
varisExisted=false;
$.each(param,function(i,n){
if(n.value.sort().join(",")==check.sort().join(",")){
isExisted=true;
returni;
}
});
if(!isExisted){
alert("chooseerror");
}
};
judge(check,param);
㈡ 如何判断JSON数组总是否包含某条JSON数据
在js 里没法直接判断,必须要循环一次判断,C#中的contain 方法也是要遍历只是.net Framework 帮你封装了
㈢ JS中如何判断JSON数据中有没有某个节点
那你就一层一层的判断,先判断input是否存在,如果存在就在input里面查找age,不存在就自
㈣ js 中如何知道一个对象里有多少数组(数量)
设置一个全局变量对数据进行循环 判断数据类型为数组 全局变量+1
或者去检索“[”并且计算出数量
㈤ js遍历json数组,求大神指导(2)
<script src="http://vod.xunlei.com/library/jquery-1.7.2.min.js"></script>
<script>
var json1=[{"age":5,"num":2,"people":3,"class":9},{"age":1,"num":3,"people":2,"class":8},{"age":4,"num":6,"people":5,"class":3}];
var json2=[{"field":"age","typle":"sum","title":"ss"},{"field":"num","typle":"sum","title":"ss"},{"field":"people","typle":"sum","title":"ss"},{"field":"class","typle":"nosum","title":"ss"}];
//如上json1、json2两个数组,拿json1里age、num等属性去json2里遍历,若json2里field的值和json1的属性相等并且其typle的值为num,就计算json1里该属性的和最终再拼接成下面的样子
//var json3=[{"name":"求和","age":10,"num":11,"people":10}];
function test(json1,json2){
var result=[],resultJson={"name":"求和"};
var getSum=function(field){
var sum=0;
for(var s in json1){
sum+=json1[s][field]||0;
}
return sum;
}
for(var p in json2){
if(json2[p]["typle"]=="sum"){
$.extend(resultJson,$.parseJSON('{\"'+ json2[p]["field"] + '\":' + getSum(json2[p]["field"]) + '}'));
}
}
result.push(resultJson);
return result;
}
json3=test(json1,json2);
//检查一下结果
temp=""
for (var t in json3[0]){
temp+=t + "=" + json3[0][t] +",";
}
alert(temp.slice(0,-1)); //显示:name=求和,age=10,num=11,people=10
</script>
㈥ js如何操作多维json数组
vardata='{"data-2014":{"msg1":"111","msg2":"222","num":2}}';
json=eval("("+data+")");
alert(json["data-2014"]["num"]);
以上代码IE和FF均通过。 先看看你的 "date"+day 值是否正确,然后再看是否乱码影响了回json解析,如果是则需要编码答。
㈦ js遍历json数组,求大神指导
<!doctypehtml>
<htmllang="en">
<head>
<metacharset="UTF-8">
<metaname="Generator"content="EditPlus®">
<metaname="Author"content="">
<metaname="Keywords"content="">
<metaname="Description"content="">
<title>Document</title>
</head>
<body>
</body>
<script>
varjson1=[
{"age":5,"num":2,"people":3,"class":9},
{"age":1,"num":3,"people":2,"class":8},
{"age":4,"num":6,"people":5,"class":3}
],
json2=[
{"field":"age","typle":"sum","title":"ss"},
{"field":"num","typle":"sum","title":"ss"},
{"field":"people","typle":"sum","title":"ss"},
{"field":"class","typle":"nosum","title":"ss"}
],
typeArr={'sum':'sum','nosum':''};
functiongetData(json1,json2,typeArr){
varj=0,
len1=json1.length,
len2=json2.length,
o={},
json3=[];
for(;j<len2;j++){
varj2=json2[j];
if(j2.field){
vartype=typeArr[j2.typle],
num=-1,
i=0;
for(varii=0;ii<json3.length;ii++){
varj3=json3[ii];
if(j3.type===type){
o=clone(j3);
num=ii;
}
}
for(;i<len1;i++){
varj1=json1[i];
if(type==='sum'){
o[j2.field]=o[j2.field]?o[j2.field]+j1[j2.field]:j1[j2.field];
}
}
if(type){
o.type=type;
if(num!==-1){
json3[num]=o;
o={};
}else{
json3[json3.length]=o;
}
}
}
}
returnjson3;
}
//深
functionclone(o){
vark,
ret=o,
b;
if(o&&((b=(oinstanceofArray))||oinstanceofObject)){
ret=b?[]:{};
for(kino){
if(o.hasOwnProperty(k)){
ret[k]=clone(o[k]);
}
}
}
returnret;
}
//getresult
console.log(getData(json1,json2,typeArr));
</script>
</html>
㈧ json数组和普通数组如何判断区分
const array = [{"alarmno":"83456", "deviceid":"358", "devicename":"设备名称"},{"alarmno":"83456", "deviceid":"358", "devicename":"设备名称"},{"alarmno":"83456", "deviceid":"358", "devicename":"设备名称"},{"alarmno":"83456", "deviceid":"358", "devicename":"设备名称"}]
const elemFirst = array[0]
if(typeof elemFirst === 'object'){
console.log('对象数组')
} else {
console.log('值数组')
}
取出数组中第一回个元素, 判断下类型就可以了答
㈨ JS怎么把N个JSON数组进行比较,相同的合并后放进新数组,不同的一起放进新数组
如果两个json字段相同,拼接起来的话 后一个json会覆盖前一个json的字段。
如果想保留2个json的数据 就把这2个json放到一个数组里吧。
分别给出代码:
拼接,字段相同会覆盖:
<scripttype="text/javascript">
varj1={
'':'',
action:'adser',
user:飓',
pwd:',
rpassword:',
email:[email protected]'
}
varj2={
aa:'aa',
user:'user'
}
json=eval('('+(JSON.stringify(j1)+JSON.stringify(j2)).replace(/}{/,',')+')');
console.log(json);
</script>
<scripttype="text/javascript">
varj1={
'':'',
action:'adser',
user:飓',
pwd:',
rpassword:',
email:[email protected]'
}
varj2={
'':餶',
action:'adser222',
user:',
pwd:',
rpassword:',
email:[email protected]'
}
json=[j1,j2];
console.log(json);
</script>