SMS messages on Android mobiles, are stored in databases, so I extracted, and stored them in a JSON array.
I want to group the SMS by the address (mobile number).
The structure I want to create is a two-dimension array that on each first index of the second dimendsion (value 0, matrix[index][0]) is stored the number and on the other values are the sms of that number.
The error is that after storing 2 numbers, the other numbers are not getting stored and the sms received by them are stored on the second number array.
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var sms = {
'0' : {
'address' : '+1',
'body' : 'SMS1'
},
'1' : {
'address' : '+2',
'body' : 'SMS2'
},
'2' : {
'address' : '+1',
'body' : 'SMS3'
},
'3' : {
'address' : '+2',
'body' : 'SMS4'
},
'4' : {
'address' : '+3',
'body' : 'SMS5'
},
'5' : {
'address' : '+4',
'body' : 'SMS5'
}
};
var matrix = [];
var html = "";
var actualBODY = "";
var actualADDRESS ="";
var matrixDOneSize =0;
var matrixDTwoSize =0;
var indexOfValue =0;
var pinpoint =-10;
for(i in sms) {
if (sms.hasOwnProperty(i)) {
actualBODY = sms[i]['body'];
actualADDRESS = sms[i]['address'];
if(i==0){
matrix[0]=[];
matrix[0][0]=actualADDRESS;
matrix[0][1]=actualBODY;
}
else {
matrixDOneSize = matrix.length;
for(var j=0;j<matrixDOneSize;j++){
indexOfValue = matrix[j][0].indexOf(actualADDRESS);
if(indexOfValue>-1){
pinpoint = j;
}
}
if(pinpoint>-10) {
matrixDTwoSize = matrix[pinpoint].length;
matrix[pinpoint][matrixDTwoSize]=actualBODY;
}
else {
matrix[matrixDOneSize]=[];
matrix[matrixDOneSize][0]=actualADDRESS;
matrix[matrixDOneSize][1]=actualBODY;
}
}
}
}
var nexti = matrix.length;
html+="Total numbers ="+nexti;
for(var i=0;i<nexti;i++){
html+= "<ul>Mobile Number = "+ matrix[i][0];
var nextj = matrix[i].length;
var nrsms = nextj-1;
html+= "<li>Nr of SMS= "+nrsms+"</li>";
for(var j=1;j<nextj;j++){
html+= "<li>SMS = "+ matrix[i][j]+"</li>";
}
html+="</ul>";
}
document.getElementById("demo").innerHTML = html;
</script>
</body>
</html>
Aucun commentaire:
Enregistrer un commentaire