㈠ 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>