Reputation: 217
My JSON object is
"msg"=[{"userName":"Mandy","emailId":"[email protected]","userCreated":"2011-12-21 17:21:49","allowedDownloads":"15"},{"userName":"Andy","emailId":"[email protected]","userCreated":"2011-12-21 17:29:58","allowedDownloads":"45"},{"userName":"Randy","emailId":"[email protected]","userCreated":"2012-01-02 10:18:19","allowedDownloads":"15"},{"userName":"Vandy","emailId":"[email protected]","userCreated":"2012-01-02 15:49:20","allowedDownloads":"14"},{"userName":"Sandy","emailId":"[email protected]","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":"[email protected]","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":"[email protected]","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" ?
Upvotes: 0
Views: 4168
Reputation: 1569
1) You can add one more record with:
data.push({"userName" : "Smit","emailId":"[email protected]","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!
Upvotes: 1
Reputation: 25165
As you have a Javascript array
var msg = [{"userName":"Mandy","emailId":"[email protected]","userCreated":"2011-12-21 17:21:49","allowedDownloads":"15"},
{"userName":"Andy","emailId":"[email protected]","userCreated":"2011-12-21 17:29:58","allowedDownloads":"45"},
{"userName":"Randy","emailId":"[email protected]","userCreated":"2012-01-02 10:18:19","allowedDownloads":"15"},
{"userName":"Vandy","emailId":"[email protected]","userCreated":"2012-01-02 15:49:20","allowedDownloads":"14"},
{"userName":"Sandy","emailId":"[email protected]","userCreated":"2012-01-02 16:47:35","allowedDownloads":"14"}];
you can easily push a new element
msg.push({"userName":"Wendy","emailId":"[email protected]","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;
}
Upvotes: 1