Hi I have an application that I have got half working. I have an array of objects, each with their properties already set and can call them like this myarray[i].property. I have an if statment that searchs through the array, within a loop, and pulls out any where myarray[i].property == my var.
The issue I'm having is that I want to put these results into a new array, built by the if statment/loop combo that searchs the first array, and I can't make it work.
This is what I have tried, but failed with?
var c = 0;
var matches = new Array('application', 'sclass', 'type', 'motor', 'bearings', 'gears', 'modelno', 'name', 'speed', 'v3_3', 'v4_8', 'v6_0', 'v7_2', 'weight', 'diensions', 'opvoltage', 'image', 'description');
//loop through servos array and pull any servo that has a matching application value to that selected by the search filter
for(var i=0; i < servos.length; i++){
if servos[i].application == document.searchFilters.applicationMenu.value) {
//populate the new 'matches' array with the details from the servos pulled from the inital arary
matches[c] = new servo(servos[i].application, servos[i].sclass, servos[i].type, servos[i].motor, servos[i].bearings, servos[i].gears, servos[i].modelno, servos[i].name, servos[i].speed, servos[i].v3_3, servos[i].v4_8, servos[i].v6_0, servos[i].v7_2, servos[i].weight, servos[i].dimensions, servos[i].opvoltage, servos[i].image, servos[i].description);
c++;
} else if (document.searchFilters.applicationMenu.value == 0){
//sets the value of servoDtore locally
var servoStore = 0;}
Further in the code I have the line document.getElementById('servoDisplay').innerHTML = "search result " + matches[c].modelno; //display servos model numbers stored within the matches array
Where am I going wrong, why do I always get '.modelno is null or undefined' errors whenever I try to call matches[c].modelno?
Let me try. Please tell me if I understood you incorrectly. I have modifyed your JS code to the following:
var matches = ['application', 'sclass', 'type', 'motor',
'bearings', 'gears', 'modelno', 'name', 'speed',
'v3_3', 'v4_8', 'v6_0', 'v7_2', 'weight',
'dimensions', 'opvoltage', 'image', 'description'],
output = [],
modelnos = [];
// c variable is unnecessary now
// Loop through servos array and pull any servo that has a matching application value to that selected by the search filter
for(var i = 0, len = servos.length; i < len; i+= 1) {
if (document.searchFilters.applicationMenu.value === servos[i].application) {
// Populate the new 'matches' array with the details from the servos pulled from the inital arary
var newEntry = new servo(servos[i].application, servos[i].sclass, servos[i].type, servos[i].motor,
servos[i].bearings, servos[i].gears, servos[i].modelno, servos[i].name, servos[i].speed,
servos[i].v3_3, servos[i].v4_8, servos[i].v6_0, servos[i].v7_2, servos[i].weight,
servos[i].dimensions, servos[i].opvoltage, servos[i].image, servos[i].description);
output.push(newEntry);
modelnos.push(newEntry.modelno);
// c++;
} else if (document.searchFilters.applicationMenu.value === 0) {
var servoStore = 0;
}
}
// Display servos model numbers stored within the matches array
document.getElementById('servoDisplay').innerHTML = "Search result: " + modelnos.join('<br />');
Related
I have a multidimensional array created by taking data from a google spreadsheet. I am attempting to seperate out the data based on results in a specific column. For example:
var j = {
["Mine", "House"],
["Your", "House"],
["his", "apt"]
}
Given that we want to seperate by column 2. We should get in theory:
var new = {
[["Mine", "House"] , ["Your", "House"]],
[["his", "apt"]]
}
Two entries being a house, and 1 being an apartment. I am haveing a huge issue with treating each entry as its own obj. Assuming it is even possible. I guess we would access specific parts of each object like so, new[0][1][1]? This obviously shouldnt work like this. Is there another way to seperate the entries in the way I am attempting? As it stands right now, I believe my code is just creating the same number of rows as in the original data.
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.insertSheet("Report");
var data_sheet =spreadsheet.getSheetByName("Data");
var genCounsel_data = data_sheet.getRange(240, 1, 96, 7).getValues(); //get genCounseling data
var report_sheet = spreadsheet.getSheetByName("Report");
//setup key values for columns in report sheet
report_sheet.appendRow(["Student Service", "Unit Student Service Outcome", "Indicators Data Sources", "How indicator was measured", "Benchmark Data", "Assigned to do Assessment", "Email"])
//seperate out by SS outcomes
var genCounselDataByOutcomes = new Array(new Array()); //all responses for each outcome, also parrellel
var outcomes_freq = new Array(); //parrellel arrays
var found = false;
//get services and frequency;
for(var i=0; i<genCounsel_data.length; ++i){
genCounsel_data[i][OUTCOMES_COL].toString().toLowerCase();
for(var j=0; j<genCounselDataByOutcomes.length && !found; ++j){
if(genCounselDataByOutcomes[j][OUTCOMES_COL] == genCounsel_data[i][OUTCOMES_COL]){
genCounselDataByOutcomes[j].push(genCounsel_data[i]); //ADD to row with same outcomes
++outcomes_freq[j]; //update amount of entries in said outcome
found = true;
}
}
if(found == false){
genCounselDataByOutcomes.push(new Array);
genCounselDataByOutcomes[genCounselDataByOutcomes.length-1].push([genCounsel_data[i]]);
outcomes_freq.push(1);
}
else
found = false;
}
for(var i=0; i<outcomes_freq.length;++i)
Logger.log(outcomes_freq[i]);
//for each outcome select a random one and move entire row to sheet;
for(var i=0; i<genCounselDataByOutcomes.length; ++i){
Logger.log(genCounselDataByOutcomes[i]);
}
QUESTION:
How can I seperate my data as multiple objects in a row of an array and be able to access specific components of each entry as shown in my example above? If this is not exactly possible in this way, is there another solution to this issue?
First of all, your j and new (which by the way is not a valid var name) need a key if they are objects or to be used as array, like below:
var j = [
["Mine", "House"],
["Your", "House"],
["his", "apt"]
];
var newVar = [
[["Mine", "House"] , ["Your", "House"]],
[["his", "apt"]]
];
That said and fixed, you can iterate over your array of arrays and use the column you want to use as filter to get the unique values to be used to group the final result.
Here is the final result:
var j = [
["Mine", "House"],
["Your", "House"],
["his", "apt"]
];
var uniqueValues = [];
var indexColumn = 1; //Here you specify the column you want to use to filter/group the elements from j
j.forEach(function(element){
if(!uniqueValues.includes(element[indexColumn])){
uniqueValues.push(element[indexColumn]);
}
});
//Now you use the `uniqueValues` to filter the `j` array and generate the `groupedArrays` array, which is the one you are looking for:
var groupedArrays = [];
uniqueValues.forEach(function(uniqueValue){
groupedArrays.push(j.filter(function(element){
return element[indexColumn] === uniqueValue;
}));
});
console.log(groupedArrays);
I hope this helps you.
Good luck.
I am using ajax to call DB and using DOM to create the HTML for a small weather widget.
So I have an Array of element called _weather.
Its populate fine, however I am having touble to refresh the data in the array.
I have a button that if clicked call this function and pass the city name _town
My idea is to remove the value from array and call the function _checkNewTown to display the city I just removed from the array.
var _update_city = function(_town){
ntown = _town;
town = _town;
_weather.prototype.removeByValue = function (town) {
for (var i = 0; i < this.length; i++) {
var c = this[i];
if (c == town || (town.equals && town.equals(c))) {
this.splice(i, 1);
break;
}
}
};
_checkNewTown(ntown);
}
However its not working and its returning
SCRIPT5007: Unable to set value of the property 'removeByValue': object is null or undefined
weather_widget.js, line 121 character 3
I tried change it but could not figure out what to use.
Any help will be apprecciated
You can use Array.prototype.filter to remove items from an array like so:
var weather = [{city: 'city1', ....}, ....];
var city = 'city1';
var filtered = weather.filter(function (i) {
return i.city !== city;
});
I have a small json
var StoreJSON = [
{
"Store_ID": "46305",
"inv_list_id": "list-46305-inventory"
},
{
"Store_ID": "48760",
"inv_list_id": "list-9980-inventory"
},
{
"Store_ID": "48811",
"inv_list_id": "list-46305-inventory"
},
{
"Store_ID": "53046",
"inv_list_id": "list-1272-inventory"
}
]
I am trying to loop through it and then retrieve content for each 'list-46305-inventory'
I think I am doing something fundamentally wrong with my loop.
var strInvID = new Array();
var invList = '';
var record = '';
for(var i = 0; i<= StoreJSON.length; i++){
strInvID[i]= StoreJSON[i].inv_list_id;
invList = ProductInventoryMgr.getInventoryList(strInvID[i]); // retriving the list
record += invList.getRecord(26551826); // getting the product record
}
document.write(record);
//
ProductInventoryMgr.getInventoryList() is a custom java method which returns the content inside the list
//
Problem: the loop is stopping at the first position. I am getting the output from the first inv_list_id but its not giving me all the others
Let me know I need to add more clarification to the question.
'26551826' is a product ID which is present in all those lists
I apologise if this has been asked before but I can't seem to find a solution from other posts on here.
I'm trying to build a json array in local storage (which is fine) but want to check if an entry already exists before adding new values.
The Json itself
[{"title":"title1","url":"somefile1.pdf","background":"bg1.png"},
{"title":"title2","url":"somefile2.pdf","background":"bg2.png"},
{"title":"title3","url":"somefile3.pdf","background":"bg3.png"}]
Now how would I query the array to ensure only unique entries are being added?
Heres the code to add to array with
var oldItems = JSON.parse(localStorage.getItem('itemsArray')) || [];
var newItem = {
'title': title,
'url': url,
'background': background
};
// Need to check the newItem is unique here //
oldItems.push(newItem);
localStorage.setItem('itemsArray', JSON.stringify(oldItems));
I thought I could use the jquery unique function instead before setting the localstorage object
var cleanedItems = $.unique(oldItems);
localStorage.setItem('itemsArray', JSON.stringify(cleanedItems));
but that didnt work...
You will have to loop over each of the items in the array that is parsed from local storage and perform an object equality test with the new item.
Object equality testing is not as simple as obj1 == obj2.
Here are some references to get you started
http://procbits.com/2012/01/19/comparing-two-javascript-objects
https://github.com/joyent/node/blob/e4cef1a0833e6d677298600e205a142d15639bf2/lib/assert.js#L205-L247
http://stamat.wordpress.com/2013/06/22/javascript-object-comparison/
http://underscorejs.org/docs/underscore.html#section-84
The following may end up working for you, by using JSON.stringify to compare the new object as a JSON string with the objects in the old array as JSON strings.
function objInArr(newObj, oldItems) {
var newObjJSON = JSON.stringify(newObj);
for (var i = 0, l = oldItems.length; i < l; i++) {
if (JSON.stringify(oldItems[i]) === newObjJSON) {
return true;
}
}
return false;
}
var oldItems = JSON.parse(localStorage.getItem('itemsArray')) || [];
var newItem = {
'title': title,
'url': url,
'background': background
};
// Need to check the newItem is unique here
if (!objInArr(newItem, oldItems)) {
oldItems.push(newItem);
}
localStorage.setItem('itemsArray', JSON.stringify(oldItems));
I am receiving a html form. This works fine when 2 or more elements in array, but when only one element is received I get error users[t] is null in fireBug?
var users = form.elements["uname[]"];
for(t in users) {
dataString += "User: "+users[t].value+"\n"
}
this solved it:
if( typeof users.value === 'string' ) {
users = [ users ];
}
I know this is an old question but I stumbed across it while searching for something else. Anyway, I thought I'd provide another answer for anyone else who stumbled across this.
Rather than checking the type to see if it is an array or not and then optionally encasing the value in a new array, you can use Array.prototype.concat().
Its syntax is
var new_array = old_array.concat(value1[, value2[, ...[, valueN]]])
where any of those values can be either an array or a single value.
In your specific case, you can start with an empty array and concatenate your form input, which will work whether you get a single value or an array:
var users = [].concat(form.elements["uname[]"]);
or
users = [].concat(users);
You could check if the variable is a string and convert it to an array:
if( typeof users === 'string' ) {
users = [ users ];
}
For iterating arrays for-in should be avoided, that statement is meant to enumerate object properties. You could try using a better loop, something like:
var userCount = users.length;
for (var i = 0; i < userCount; i++) {
dataString += "User: "+users[i].value+"\n"
}
You could also base a test on the length. If the object is single it will return undefined for length.
var userCount = users.length; //Get user count
if ( userCount == undefined ) { //Returned undefined if not an array.
users = [ users ]; //Convert to array
}