My JSON object is
"msg"=[{"userName":"Mandy","emailId":"m@t.co","userCreated":"2011-12-21 17:21:49","allowedDownloads":"15"},{"userName":"Andy","emailId":"ab@r.co","userCreated":"2011-12-21 17:29:58","allowedDownloads":"45"},{"userName":"Randy","emailId":"re@t.co","userCreated":"2012-01-02 10:18:19","allowedDownloads":"15"},{"userName":"Vandy","emailId":"vai@t.co","userCreated":"2012-01-02 15:49:20","allowedDownloads":"14"},{"userName":"Sandy","emailId":"vrush@t.co","userCreated":"2012-01-02 16:47:35","allowedDownloads":"14"}]
1)How do I add 1 more person so that "msg" is appended with
{"userName":"Wendy","emailId":"w@t.co","userCreated":"2012-12-21 17:21:49","allowedDownloads":"15"}
2) How do I add a property "hobbies" to each of these indexes, so that I have,for eg
{"userName":"Wendy","emailId":"w@t.co","userCreated":"2012-12-21 17:21:49","allowedDownloads":"15","hobbies":"skiing,football,hockey"}
3) How do I check whether the index "Wendy" has a hobby "hockey" ?
As you have a Javascript array
var msg = [{"userName":"Mandy","emailId":"m@t.co","userCreated":"2011-12-21 17:21:49","allowedDownloads":"15"},
{"userName":"Andy","emailId":"ab@r.co","userCreated":"2011-12-21 17:29:58","allowedDownloads":"45"},
{"userName":"Randy","emailId":"re@t.co","userCreated":"2012-01-02 10:18:19","allowedDownloads":"15"},
{"userName":"Vandy","emailId":"vai@t.co","userCreated":"2012-01-02 15:49:20","allowedDownloads":"14"},
{"userName":"Sandy","emailId":"vrush@t.co","userCreated":"2012-01-02 16:47:35","allowedDownloads":"14"}];
you can easily push a new element
msg.push({"userName":"Wendy","emailId":"w@t.co","userCreated":"2012-12-21 17:21:49","allowedDownloads":"15"});
but to update a record you have to loop
function update(username, property, value){
for(var i=0; i < msg.length; i++){
var user = msg[i];
if(user["userName"] == username){
user[property] = value;
break;
}
}
}
to search also you have to loop
function check(username, property, value){
for(var i=0; i < msg.length; i++){
var user = msg[i];
if(user["userName"] == username){
var propertyVal = user[property];
if( propertyVal && propertyVal.indexOf(value) != -1){
return true;
}
}
}
return false;
}
1) You can add one more record with:
data.push({"userName" : "Smit","emailId":"smit@example.com","userCreated":"2011-12-21 17:29:58","allowedDownloads":"9"});
2) You can add "hobby" like:
for(a in data)
{
data[a].hobbies = "skiing,football,hockey";
}
3) For the last question, you can create a function. I'm not so good with javascript, so there might be some other option apart from this. But you can start with this code:
function getHobbey(userName, hobbey_name)
{
for(a in data)
{
if (data[a].userName == userName)
{
var hb = data[a].hobbies;
if (hb != '')
{
all_hb = hb.split(",");
for(i=0; i<= all_hb.length; i++)
{
if (all_hb[i] == hobbey_name)
{
return true;
}
}
return false;
}
return false;
}
}
}
And by calling that
alert(getHobbey("Smit","skiing"));
Will give you true or false.
Still there are lots of thing in which you can improve this function.
Thanks!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.