jQuery each in a each? - javascript
I will create a dynamic webshop product loader and have this i array output from my system.
{"product":[{"picture":".jpg","title":"test","category":"varenummer","price":"500,00 kr.","onStock":"1","status":"test"},{"picture":".jpeg","title":"test test","category":"varenummerssss","price":"550,00 kr.","onStock":"5","status":"test s"}]}
i don't know how i can create this each loop out.
var data = {"product":[{"picture":".jpg","title":"test","category":"varenummer","price":"500,00 kr.","onStock":"1","status":"test"},{"picture":".jpeg","title":"test test","category":"varenummerssss","price":"550,00 kr.","onStock":"5","status":"test s"}]}
$.each(data.product, function() {
$.each(this, function(key, value) {
// key = picture, title, etc
// value = .jpg, test, etc
});
});
in jQuery.each the second argument is set as this for the scope. So this === arguments[1]
jQuery is overkill to loop through a list.
Fairly straight forward and very simplified:
var list = {"product":[{"picture":".jpg","title":"test","category":"varenummer","price":"500,00 kr.","onStock":"1","status":"test"},{"picture":".jpeg","title":"test test","category":"varenummerssss","price":"550,00 kr.","onStock":"5","status":"test s"}]};
var i, len, product;
for(i = 0, len = list.product.length; i < len; i++)
{
product = list.product[i];
alert(product.picture + " and " + product.title);
}
Example
Related
Pull information from a data file?
I have over 170000 entries of data in data file in this format: 1479661:-1,1,-1,-898,-769,0.00;-1,2,-1,-96,-1402,0.00;-1,3,-1,117,-1397,0.00;-1,4,-1,-4,-2420,0.00;4,5,-1,5570,4395,0.00;4,6,-1,5570,4395,0.00;4,7,-1,5570,4395,0.00;4,8,-1,5570,4395,0.00;4,9,-1,5570,4395,0.00;4,10,-1,5570,4395,0.00;4,11,-1,5570,4395,0.00;4,12,-1,5570,4395,0.00;4,13,-1,5570,4395,0.00;4,14,-1,5570,4395,0.00;-1,15,-1,913,-3533,0.00;4,16,-1,5570,4395,0.00;4,17,-1,5570,4395,0.00;4,18,-1,5570,4395,0.00;4,19,-1,5570,4395,0.00;4,20,-1,5570,4395,0.00;4,21,-1,5570,4395,0.00;4,22,-1,5570,4395,0.00;4,23,-1,5570,4395,0.00;4,24,-1,5570,4395,0.00;4,25,-1,5570,4395,0.00;4,26,-1,5570,4395,0.00;4,27,-1,5570,4395,0.00;4,28,-1,5570,4395,0.00;4,29,-1,5570,4395,0.00;:117,-1397,7,7.00,A,Dead;: 1479662:-1,1,-1,-898,-769,0.00;-1,2,-1,-96,-1402,0.00;-1,3,-1,117,-1392,0.00;-1,4,-1,-6,-2419,0.00;4,5,-1,5570,4395,0.00;4,6,-1,5570,4395,0.00;4,7,-1,5570,4395,0.00;4,8,-1,5570,4395,0.00;4,9,-1,5570,4395,0.00;4,10,-1,5570,4395,0.00;4,11,-1,5570,4395,0.00;4,12,-1,5570,4395,0.00;4,13,-1,5570,4395,0.00;4,14,-1,5570,4395,0.00;-1,15,-1,913,-3533,0.00;4,16,-1,5570,4395,0.00;4,17,-1,5570,4395,0.00;4,18,-1,5570,4395,0.00;4,19,-1,5570,4395,0.00;4,20,-1,5570,4395,0.00;4,21,-1,5570,4395,0.00;4,22,-1,5570,4395,0.00;4,23,-1,5570,4395,0.00;4,24,-1,5570,4395,0.00;4,25,-1,5570,4395,0.00;4,26,-1,5570,4395,0.00;4,27,-1,5570,4395,0.00;4,28,-1,5570,4395,0.00;4,29,-1,5570,4395,0.00;:117,-1392,7,7.07,A,Dead;: Now each data array starts with a unique id and i was wondering is there a convenient way to push each entry 1479661 then 1479662 every second to an array and assign the values within the unique id into 6 fields that update. Now I ask if there is a more convenient way as currently I am using this method: Data comprises of three chunks in a single line Split the link into chunks var chunkOne = [1479661]; var chunkTwo = [-1,1,-1,-898,-769,0.00,-1,2,-1,-96,-1402,0.00,-1,3,-1,117,-1397,0.00,-1,4,-1,-4,-2420,0.00,4,5,-1,5570,4395,0.00,4,6,-1,5570,4395,0.00,4,7,-1,5570,4395,0.00,4,8,-1,5570,4395,0.00,4,9,-1,5570,4395,0.00,4,10,-1,5570,4395,0.00,4,11,-1,5570,4395,0.00,4,12,-1,5570,4395,0.00,4,13,-1,5570,4395,0.00,4,14,-1,5570,4395,0.00,-1,15,-1,913,-3533,0.00,4,16,-1,5570,4395,0.00,4,17,-1,5570,4395,0.00,4,18,-1,5570,4395,0.00,4,19,-1,5570,4395,0.00,4,20,-1,5570,4395,0.00,4,21,-1,5570,4395,0.00,4,22,-1,5570,4395,0.00,4,23,-1,5570,4395,0.00,4,24,-1,5570,4395,0.00,4,25,-1,5570,4395,0.00,4,26,-1,5570,4395,0.00,4,27,-1,5570,4395,0.00,4,28,-1,5570,4395,0.00,4,29,-1,5570,4395,0.00]; var chunkThree = [117,-1397,7,7.00,"A","Dead"]; Then get length of each array: var chunkOneLength = chunkOne.length; var chunkTwoLength = chunkTwo.length; var chunkThreeLength = chunkThree.length; Pick out the nth value in the array depending on the data chunk: //uniqueID set as first for (var i = 0; i < chunkOneLength; i = i + 1) { // useful code would go here alert("This is the unique ID " + chunkOne[i]); } //teamval for (var i = 0; i < chunkTwoLength; i = i + 6) { // useful code would go here alert("This is the teamVal " + chunkTwo[i]); } Now the only problem I see with this method, is that the original data array will need to be formatted and separated into chunks every time.
As you have a separator on each section i.e. : you can actually use split to split them up like below and push them into a array and only have to do 2 loops - firstly pushing the split data in a new array and then looping through them and populating the structure. Please note as long as each chunk of data is separated with : this will work with anything even if you expand the data later. obviously it will not work if you remove the : separator. example below: var splitChucks = []; var chucks = [ "1479661:-1,1,-1,-898,-769,0.00;-1,2,-1,-96,-1402,0.00;-1,3,-1,117,-1397,0.00;-1,4,-1,-4,-2420,0.00;4,5,-1,5570,4395,0.00;4,6,-1,5570,4395,0.00;4,7,-1,5570,4395,0.00;4,8,-1,5570,4395,0.00;4,9,-1,5570,4395,0.00;4,10,-1,5570,4395,0.00;4,11,-1,5570,4395,0.00;4,12,-1,5570,4395,0.00;4,13,-1,5570,4395,0.00;4,14,-1,5570,4395,0.00;-1,15,-1,913,-3533,0.00;4,16,-1,5570,4395,0.00;4,17,-1,5570,4395,0.00;4,18,-1,5570,4395,0.00;4,19,-1,5570,4395,0.00;4,20,-1,5570,4395,0.00;4,21,-1,5570,4395,0.00;4,22,-1,5570,4395,0.00;4,23,-1,5570,4395,0.00;4,24,-1,5570,4395,0.00;4,25,-1,5570,4395,0.00;4,26,-1,5570,4395,0.00;4,27,-1,5570,4395,0.00;4,28,-1,5570,4395,0.00;4,29,-1,5570,4395,0.00;:117,-1397,7,7.00,A,Dead;:", "1479662:-1,1,-1,-898,-769,0.00;-1,2,-1,-96,-1402,0.00;-1,3,-1,117,-1392,0.00;-1,4,-1,-6,-2419,0.00;4,5,-1,5570,4395,0.00;4,6,-1,5570,4395,0.00;4,7,-1,5570,4395,0.00;4,8,-1,5570,4395,0.00;4,9,-1,5570,4395,0.00;4,10,-1,5570,4395,0.00;4,11,-1,5570,4395,0.00;4,12,-1,5570,4395,0.00;4,13,-1,5570,4395,0.00;4,14,-1,5570,4395,0.00;-1,15,-1,913,-3533,0.00;4,16,-1,5570,4395,0.00;4,17,-1,5570,4395,0.00;4,18,-1,5570,4395,0.00;4,19,-1,5570,4395,0.00;4,20,-1,5570,4395,0.00;4,21,-1,5570,4395,0.00;4,22,-1,5570,4395,0.00;4,23,-1,5570,4395,0.00;4,24,-1,5570,4395,0.00;4,25,-1,5570,4395,0.00;4,26,-1,5570,4395,0.00;4,27,-1,5570,4395,0.00;4,28,-1,5570,4395,0.00;4,29,-1,5570,4395,0.00;:117,-1392,7,7.07,A,Dead;:"]; for (var i = 0; i < chucks.length; i++){ splitChucks.push(chucks[i].split(':')) } for (var h = 0; h < splitChucks.length; h++) { alert("This is the unique ID " + splitChucks[h][0]); alert("This is the teamVal " + splitChucks[h][1]); } Hope this helps, this is a much more efficient way to do your task :)
var splitChucks = []; var chucks = [ "1479661:-1,1,-1,-898,-769,0.00;-1,2,-1,-96,-1402,0.00;-1,3,-1,117,-1397,0.00;-1,4,-1,-4,-2420,0.00;4,5,-1,5570,4395,0.00;4,6,-1,5570,4395,0.00;4,7,-1,5570,4395,0.00;4,8,-1,5570,4395,0.00;4,9,-1,5570,4395,0.00;4,10,-1,5570,4395,0.00;4,11,-1,5570,4395,0.00;4,12,-1,5570,4395,0.00;4,13,-1,5570,4395,0.00;4,14,-1,5570,4395,0.00;-1,15,-1,913,-3533,0.00;4,16,-1,5570,4395,0.00;4,17,-1,5570,4395,0.00;4,18,-1,5570,4395,0.00;4,19,-1,5570,4395,0.00;4,20,-1,5570,4395,0.00;4,21,-1,5570,4395,0.00;4,22,-1,5570,4395,0.00;4,23,-1,5570,4395,0.00;4,24,-1,5570,4395,0.00;4,25,-1,5570,4395,0.00;4,26,-1,5570,4395,0.00;4,27,-1,5570,4395,0.00;4,28,-1,5570,4395,0.00;4,29,-1,5570,4395,0.00;:117,-1397,7,7.00,A,Dead;:", "1479662:-1,1,-1,-898,-769,0.00;-1,2,-1,-96,-1402,0.00;-1,3,-1,117,-1392,0.00;-1,4,-1,-6,-2419,0.00;4,5,-1,5570,4395,0.00;4,6,-1,5570,4395,0.00;4,7,-1,5570,4395,0.00;4,8,-1,5570,4395,0.00;4,9,-1,5570,4395,0.00;4,10,-1,5570,4395,0.00;4,11,-1,5570,4395,0.00;4,12,-1,5570,4395,0.00;4,13,-1,5570,4395,0.00;4,14,-1,5570,4395,0.00;-1,15,-1,913,-3533,0.00;4,16,-1,5570,4395,0.00;4,17,-1,5570,4395,0.00;4,18,-1,5570,4395,0.00;4,19,-1,5570,4395,0.00;4,20,-1,5570,4395,0.00;4,21,-1,5570,4395,0.00;4,22,-1,5570,4395,0.00;4,23,-1,5570,4395,0.00;4,24,-1,5570,4395,0.00;4,25,-1,5570,4395,0.00;4,26,-1,5570,4395,0.00;4,27,-1,5570,4395,0.00;4,28,-1,5570,4395,0.00;4,29,-1,5570,4395,0.00;:117,-1392,7,7.07,A,Dead;:"]; for (var i = 0; i < chucks.length; i++){ splitChucks.push(chucks[i].split(':')) } for (var h = 0; h < splitChucks.length; h++) { alert("This is the unique ID " + splitChucks[h][0]); alert("This is the teamVal " + splitChucks[h][1]); }
One of the best purposes of an unique ID is to act as an index. So, instead of iterating over your index array (chunkOne), use ID as an Object key! // 1479661:-1,1,-1,-898,-769,0.00;-1,2,-1,-96,-1402,0.00;-1,3,-1,117,-1397,0.00;-1,4,-1,-4,-2420,0.00;4,5,-1,5570,4395,0.00;4,6,-1,5570,4395,0.00;4,7,-1,5570,4395,0.00;4,8,-1,5570,4395,0.00;4,9,-1,5570,4395,0.00;4,10,-1,5570,4395,0.00;4,11,-1,5570,4395,0.00;4,12,-1,5570,4395,0.00;4,13,-1,5570,4395,0.00;4,14,-1,5570,4395,0.00;-1,15,-1,913,-3533,0.00;4,16,-1,5570,4395,0.00;4,17,-1,5570,4395,0.00;4,18,-1,5570,4395,0.00;4,19,-1,5570,4395,0.00;4,20,-1,5570,4395,0.00;4,21,-1,5570,4395,0.00;4,22,-1,5570,4395,0.00;4,23,-1,5570,4395,0.00;4,24,-1,5570,4395,0.00;4,25,-1,5570,4395,0.00;4,26,-1,5570,4395,0.00;4,27,-1,5570,4395,0.00;4,28,-1,5570,4395,0.00;4,29,-1,5570,4395,0.00;:117,-1397,7,7.00,A,Dead;: var data = { 1479661: [ -1,1,-1,-898,-769,0.00,-1,2,-1,-96,-1402,0.00,-1,3,-1,117,-1397,0.00,-1,4,-1,-4,-2420,0.00,4,5,-1,5570,4395,0.00,4,6,-1,5570,4395,0.00,4,7,-1,5570,4395,0.00,4,8,-1,5570,4395,0.00,4,9,-1,5570,4395,0.00,4,10,-1,5570,4395,0.00,4,11,-1,5570,4395,0.00,4,12,-1,5570,4395,0.00,4,13,-1,5570,4395,0.00,4,14,-1,5570,4395,0.00,-1,15,-1,913,-3533,0.00,4,16,-1,5570,4395,0.00,4,17,-1,5570,4395,0.00,4,18,-1,5570,4395,0.00,4,19,-1,5570,4395,0.00,4,20,-1,5570,4395,0.00,4,21,-1,5570,4395,0.00,4,22,-1,5570,4395,0.00,4,23,-1,5570,4395,0.00,4,24,-1,5570,4395,0.00,4,25,-1,5570,4395,0.00,4,26,-1,5570,4395,0.00,4,27,-1,5570,4395,0.00,4,28,-1,5570,4395,0.00,4,29,-1,5570,4395,0.00,117,-1397,7,7.00,"A","Dead" ] // More data... }; Object.keys(data).forEach(function(key) { console.log('This is ID ' + key); data[key].forEach(function(value,index,array) { console.log('Index ' + index + ' of data key ' + key + ':'); console.log(data[key][index]); }); }); Test this on any modern browser's console or node.js instance to see results.
javascript: limit number of items
sorry but I'm new in javascrit. I'm writing a simple rss reader, but I want to limit the number of feeds to 5 items, because the my target site can have 100 or more. Here the code: $("#mainPage").live("pageinit", function () { $("h1", this).text(title); $.get(RSS, {}, function (res, code) { var xml = $(res); var items = xml.find("item"); var items = $items.length && i < 5; i++); // here the problem!! var entry = ""; $.each(items, function (i, v) { entry = { title:$(v).find("title").text(), link:$(v).find("link").text(), description:$.trim($(v).find("encoded").text()), category:$.trim($(v).find("category").text()), date:$(v).find("pubDate").text().substr(0,16), autor:$(v).find("creator").text() }; entries.push(entry); }); var s = ''; $.each(entries, function(i, v) { s += '<li>' + v.title + '<br><i>' + v.autor + ' - ' + v.date + '</i></li>'; }); $("#linksList").append(s); $("#linksList").listview("refresh"); }); }); The problem is that when I try to limit the number of items adding var items = $items.length && i < 5; i++); the javascript stop to works. :-( How to do it?
var items = $items.length && i < 5; i++); is invalid I think you want to var items = items.slice(0, 4); is what you want. https://api.jquery.com/slice/ (as pointed out to me, it's a jQuery object) The jQuery object itself behaves much like an array; it has a length property and the elements in the object can be accessed by their numeric indices [0] to [length-1]. Note that a jQuery object is not actually a Javascript Array object, so it does not have all the methods of a true Array object such as join(). Because Javascript is 0-based (starts to count from 0) you want element 0 to 4, in order to get the first 5 elements.
You can use lt(n) function var items = xml.find("item:lt(5)");
populate select element based on json
How could I populate a second select element? I've figured out how to do the first one. But how could I do the same for the second depending on which "Make" is selected? I've tried to talk myself through it while taking small steps but I'm thinking this may be too advanced for me. var cars = '{"USED":[{"name":"Acura","value":"20001","models":[{"name":"CL","value":"20773"},{"name":"ILX","value":"47843"},{"name":"ILX Hybrid","value":"48964"},{"name":"Integra","value":"21266"},{"name":"Legend","value":"21380"},{"name":"MDX","value":"21422"},{"name":"NSX","value":"21685"},{"name":"RDX","value":"21831"},{"name":"RL","value":"21782"},{"name":"RSX","value":"21784"},{"name":"SLX","value":"21879"},{"name":"TL","value":"22237"},{"name":"TSX","value":"22248"},{"name":"Vigor","value":"22362"},{"name":"ZDX","value":"32888"}]},{"name":"Alfa Romeo","value":"20047","models":[{"name":"164","value":"20325"},{"name":"8c Competizione","value":"34963"},{"name":"Spider","value":"22172"}]}'; var carobj = eval ("(" + cars + ")"); var select = document.getElementsByTagName('select')[0]; //print array elements out for (var i = 0; i < carobj.USED.length; i++) { var d = carobj.USED[i]; select.options.add(new Option(d.name, i)) };
If I read your question right, you want to populate a second select with the models for the make in the first select. See below for a purely JS approach (with jsfiddle). If possible, I would recommend looking into jQuery, since I would prefer a jQuery solution. http://jsfiddle.net/m5U8r/1/ var carobj; window.onload = function () { var cars = '{"USED":[{"name":"Acura","value":"20001","models":[{"name":"CL","value":"20773"},{"name":"ILX","value":"47843"},{"name":"ILX Hybrid","value":"48964"},{"name":"Integra","value":"21266"},{"name":"Legend","value":"21380"},{"name":"MDX","value":"21422"},{"name":"NSX","value":"21685"},{"name":"RDX","value":"21831"},{"name":"RL","value":"21782"},{"name":"RSX","value":"21784"},{"name":"SLX","value":"21879"},{"name":"TL","value":"22237"},{"name":"TSX","value":"22248"},{"name":"Vigor","value":"22362"},{"name":"ZDX","value":"32888"}]},{"name":"Alfa Romeo","value":"20047","models":[{"name":"164","value":"20325"},{"name":"8c Competizione","value":"34963"}, {"name":"Spider","value":"22172"}]}]}'; carobj = eval ("(" + cars + ")"); var makes = document.getElementById('make'); for (var i = 0; i < carobj.USED.length; i++) { var d = carobj.USED[i]; makes.options.add(new Option(d.name, i)); } makes.onchange = getModels; getModels(); } // add models based on make function getModels () { var makes = document.getElementById('make'); var make = makes.options[makes.selectedIndex].text; for (var i = 0; i < carobj.USED.length; i++) { if (carobj.USED[i].name == make) { var models = document.getElementById('model'); models.options.length = 0; for (var j= 0; j < carobj.USED[i].models.length; j++) { var model = carobj.USED[i].models[j]; models.options.add(new Option(model.name, j)); } break; } } } I would also recommend looking into safer JSON parsing. There is a security risk in using eval if it runs on any user input. You could look into JSON.org and their json2.js. Or if you want to use jQuery: parseJSON. Below is the jQuery version: jQuery.parseJSON(jsonString); JSON parsing tips from: Safely turning a JSON string into an object.
json sibling data
(forgive me if I use slightly incorrect language - feel free to constructively correct as needed) There are a couple posts about getting data from JSON data of siblings in the returned object, but I'm having trouble applying that information to my situation: I have a bunch of objects that are getting returned as JSON from a REST call and for each object with a node of a certain key:value I need to extract the numeric value of a sibling node of a specific key. For example: For the following list of objects, I need to add up the numbers in "file_size" for each object with matching "desc" and return that to matching input values on the page. {"ResultSet":{ Result":[ { "file_size":"722694", "desc":"description1", "format":"GIF" }, { "file_size":"19754932", "desc":"description1", "format":"JPEG" }, { "file_size":"778174", "desc":"description2", "format":"GIF" }, { "file_size":"244569996", "desc":"description1", "format":"PNG" }, { "file_size":"466918", "desc":"description2", "format":"TIFF" } ] }}
You can use the following function: function findSum(description, array) { var i = 0; var sum = 0; for(i = 0; i < array.length; i++) { if(array[i]["desc"] == description && array[i].hasOwnProperty("file_size")) { sum += parseInt(array[i]["file_size"], 10); } } alert(sum); } And call it like this: findSum("description1", ResultSet.Result); To display an alert with the summation of all "description1" file sizes. A working JSFiddle is here: http://jsfiddle.net/Q9n2U/. In response to your updates and comments, here is some new code that creates some divs with the summations for all descriptions. I took out the hasOwnProperty code because you changed your data set, but note that if you have objects in the data array without the file_size property, you must use hasOwnProperty to check for it. You should be able to adjust this for your jQuery .each fairly easily. var data = {}; var array = ResultSet.Result; var i = 0; var currentDesc, currentSize; var sizeDiv; var sumItem; //Sum the sizes for each description for(i = 0; i < array.length; i++) { currentDesc = array[i]["desc"]; currentSize = parseInt(array[i]["file_size"], 10); data[currentDesc] = typeof data[currentDesc] === "undefined" ? currentSize : data[currentDesc] + currentSize; } //Print the summations to divs on the page for(sumItem in data) { if(data.hasOwnProperty(sumItem)) { sizeDiv = document.createElement("div"); sizeDiv.innerHTML = sumItem + ": " + data[sumItem].toString(); document.body.appendChild(sizeDiv); } } A working JSFiddle is here: http://jsfiddle.net/DxCLu/.
That's an array embedded in an object, so data.ResultSet.Result[2].file_size would give you 778174
var sum = {}, result = ResultSet.Result // Initialize Sum Storage for(var i = 0; i < result.length; i++) { sum[result[i].desc] = 0; } // Sum the matching file size for(var i = 0; i < result.length; i++) { sum[result[i].desc] += parseInt(result[i]["file_size"] } After executing above code, you will have a JSON named sum like this sum = { "description1": 20477629, "description2": 1246092 };
An iterate like below should do the job, var result = data.ResultSet.Result; var stat = {}; for (var i = 0; i < result.length; i++) { if (stat.hasOwnProperty(result[i].cat_desc)) { if (result[i].hasOwnProperty('file_size')) { stat[result[i].cat_desc] += parseInt(result[i].file_size, 10); } } else { stat[result[i].cat_desc] = parseInt(result[i].file_size, 10); } } DEMO: http://jsfiddle.net/HtrLu/1/
Finding an object and returning it based on search criteria
I have been searching online all day and I cant seem to find my answer. (and I know that there must be a way to do this in javascript). Basically, I want to be able to search through an array of objects and return the object that has the information I need. Example: Each time someone connects to a server: var new_client = new client_connection_info(client_connect.id, client_connect.remoteAddress, 1); function client_connection_info ( socket_id, ip_address, client_status) { this.socket_id=socket_id; this.ip_address=ip_address; this.client_status=client_status; // 0 = offline 1 = online }; Now, I want to be able to search for "client_connection.id" or "ip_address", and bring up that object and be able to use it. Example: var results = SomeFunction(ip_address, object_to_search); print_to_screen(results.socket_id); I am new to javascript, and this would help me dearly!
Sounds like you simply want a selector method, assuming I understood your problem correctly: function where(array, predicate) { var matches = []; for(var j = 0; j < array.length; j++) if(predicate(j)) matches.push(j); return matches; } Then you could simply call it like so: var sample = []; for(var j = 0; j < 10; j++) sample.push(j); var evenNumbers = where(sample, function(elem) { return elem % 2 == 0; }); If you wanted to find a specific item: var specificguy = 6; var sixNumber = where(sample, function(elem) { return elem == specificguy; });
What have you tried? Have you looked into converting the data from JSON and looking it up as you would in a dictionary? (in case you don't know, that would look like object['ip_address']) jQuery has a function for this jQuery.parseJSON(object).
You're going to need to loop through your array, and stop when you find the object you want. var arr = [new_client, new_client2, new_client3]; // array of objects var found; // variable to store the found object var search = '127.0.0.1'; // what we are looking for for(var i = 0, len = arr.length; i < len; i++){ // loop through array var x = arr[i]; // get current object if(x.ip_address === search){ // does this object contain what we want? found = x; // store the object break; // stop looping, we've found it } }