JavaScript - How Do I Sort Multi-dimension Array But From 1 To Length Not 0 To Length
My array is like so...
shp[0][0] = 5; shp[0][1] = "A5"; shp[0][2] = "A2"; shp[0][3] = "A1"; shp[0][4] = "A4"; shp[0][5] = "A3"; shp[1][0] = 4; shp[1][1] = "C3"; shp[1][2] = "C4"; shp[1][3] = "C1"; shp[1][4] = "C2"; shp[2][0] = 3; shp[2][1] = "E1"; shp[2][2] = "E3"; shp[2][3] = "E2"; shp[3][0] = 3; shp[3][1] = "G3"; shp[3][2] = "G2"; shp[3][3] = "G1"; shp[4][0] = 2; shp[4][1] = "I2"; shp[4][2] = "I1"; the results I am after is... shp[0][0] = 5; shp[0][1] = "A1"; shp[0][2] = "A2"; shp[0][3] = "A3"; shp[0][4] = "A4"; shp[0][5] = "A5"; shp[1][0] = 4; shp[1][1] = "C1"; shp[1][2] = "C2"; shp[1][3] = "C3"; shp[1][4] = "C4"; shp[2][0] = 3; shp[2][1] = "E1"; shp[2][2] = "E2"; shp[2][3] = "E3"; shp[3][0] = 3; shp[3][1] = "G1"; shp[3][2] = "G2"; shp[3][3] = "G3"; shp[4][0] = 2; shp[4][1] = "I1"; shp[4][2] = "I2"; so it sorts all from the second part of the array to the end in alpha-numerical order. I tried the following but i get errors about Cannot call method 'unshift' of undefined. // var shp; var shpbk; var shpbktemp; // shpbk = shp.slice(); shpbktemp[0] = shpbk[0][0]; shpbk[0] = shpbk[0].shift; shpbk[0] = shpbk[0].sort; shpbk[0] = shpbk[0].unshift(shpbktemp[0]); shpbktemp[1] = shpbk[1][0]; shpbk[1] = shpbk[1].shift; shpbk[1] = shpbk[1].sort; shpbk[1] = shpbk[1].unshift(shpbktemp[1]); shpbktemp[2] = shpbk[2][0]; shpbk[2] = shpbk[2].shift; shpbk[2] = shpbk[2].sort; shpbk[2] = shpbk[2].unshift(shpbktemp[2]); shpbktemp[3] = shpbk[3][0]; shpbk[3] = shpbk[3].shift; shpbk[3] = shpbk[3].sort; shpbk[3] = shpbk[3].unshift(shpbktemp[3]); shpbktemp[4] = shpbk[4][0]; shpbk[4] = shpbk[4].shift; shpbk[4] = shpbk[4].sort; shpbk[4] = shpbk[4].unshift(shpbktemp[4]); Similar TutorialsAll, I have the following code: console.log("My num is: " + num); console.log("My lat is: " + lat); console.log("My lat length is: " + lat.length); console.log("My long is: " + long); When I get the output, I get the following: My num is: {"1":1,"2":2} My lat is: {"1":"40.59479899","2":"41.4599860"} My lat length is: 36 My long is: {"1":"-75.5549650","2":"-87.596430"} I'm confused on why the length is saying 36 when it's obviously two and the other output shows that??? Hi, I'm a JS beginner and I find looping through arrays with for/in is very easy. Yet I find lots of code examples where array length is used instead of for/in and I'm thinking to myself, why do it this (somewhat) hard(er) way? Maybe I'm missing something... Thanks for your help. Hello, another noob question for you: I have a function that requires the ability to count the number of entries in an array. I'm using the following to call my function inside an input tag: Code: onblur="javascript:check(this.name, this.value, 1, 10);" which for example is calling check('field1', 'foobar', 1, 10) Here is the javascript: Code: function check(name, value, min, max){ var errors=new Array(); if(value.length < min || value.length > max){ //checking against min/max length of value errors[name] = "Text field must not be blank."; errors["blabla"] = "array value 2"; //added for testing purposes alert(name+" : "+value); //returns "field1 : foobar" } alert(errors.length); // returns "0" } And when errors.length is alerted, it outputs 0. I can only figure that there is an issue when using custom named keys in an array, since this works when I use only integers, however, I have the need to use a custom key index as shown. Any ideas? For some reason i'm getting the error of "length is null". Which is from this . Code: function display_form(id) { //Create some variables var html = ""; var container = "form_container"; var menu = load_menu(id, false); //Get id for ( var i = 0; i < menu.length; i++ ) { alert(i); } http = getHTTPObject(); http.onreadystatechange = function() { if ( http.readyState == 4 && http.status == 200 ) { document.getElementById(container).innerHTML = http.responseText; } else { document.getElemenyById(container).innerHTML = "Loading..."; } } http.open( "GET", "includes/forms.php?form=" + id ); http.send(); //Watch input fields //login_listener(); }/* Here is the function: Code: function load_menu(menu, return_d) { //Availible menus var data = []; data [ 'login' ] = [ 'Login', 'Register', 'Recovery' ]; data [ 'settings' ] = [ 'Profile', 'Account', 'something' ]; if (return_d == false) { return data[ menu ]; } else { //Menu container ID's var cons = Array('menuitem1','menuitem2','menuitem3'); for ( var i = 0; i < cons.length; i++ ) { document.getElementById(cons[i]).innerHTML += data[ menu ] [ i ]; } } } Extra info: id = login menu = login Hello. I wonder is there any javascript to calculate IP length For example I have Start ADDR: VLAN_ADDR which is: 10.52.28.0 Stop ADDR: VLAN_LAST which is 10.52.29.254 As I know:result is 512 or another example: Start ADDR: VLAN_ADDR which is: 10.52.64.11 Stop ADDR: VLAN_LAST which is 10.52.64.15 As I know:result is 5 How to count it in javascript code ? Results are "numbers" Best regards Leos. Overview: Order entry project. I am using an iFrame to pull external forms that I can not change. Once our employee enters in the information and submits I would like to redirect the existing window to a new page. I've tried many variations of: Code: <script type='text/javascript'> function iHistory() { var hist=history.length; if (hist>1) { parent.window.location = "orderentry.php"; } } </script> Since I have no control of the external form I have been using OnLoad() in the iFrame tag to call iHistory(). The script works great unless the browser history is >1 If possible, I would like to also setup like a 5 or 10 second delay so the employee can see the framed confirmation page before redirect. So the function needs to check current history.length, when that changes delay 5-10 seconds and redirect parent window to orderentry.php function checkValidFormInput() { if (document.getElementsByName('customerName').value != '') { document.getElementById('customerNameImg').innerHTML = '<img alt="Valid" src="images/greenTick.png">'; } else { document.getElementById('customerNameImg').innerHTML = '<img alt="Invalid" src="images/redX.png">'; } if (document.getElementsByName('customerEmail').length > 0) { document.getElementById('customerEmailImg').innerHTML = '<img alt="Valid" src="images/greenTick.png">'; } else { document.getElementById('customerEmailImg').innerHTML = '<img alt="Invalid" src="images/redX.png">'; } } Ok I have a textarea field that is validated by Js, this textarea can and will contain the newline character Code: \n so I validate in JS Code: if(textareaname.value.length < 200) this hten goes through to my php where i also check before I place in to the Database using MYSQL Code: if( strlen($_POST['textareaname']) < 200 ) but my php is giving me a different string length from my javascript. It looks as if Javascript is counting a newline as 1 character and php is treating it as 2. I have checked my slashes, I have used various REgex to check these data amounts. I have also Googled around and there doesnt seem much around. Any ideas? Any suggestions as to how I can make php and javascript treat a newline as the same amount of characters? Thanks in advance. I want to use the new keyword to instantiate an object. The catch is I want to pass in variable length arguments. Given: Code: function Foo () { this.args = Array.prototype.join.call (arguments); } var args = ["arg0", "arg1", "arg2"]; The following don't work (though I understand why): Code: new Foo.apply (null, args); new (Foo.apply (null, args)); (new Foo).apply (null, args); Any ideas on how to do what I want without modifying Foo's source code? I'm trying to figure out how to add a length paramater to this function. Not quite sure how to do it. here's what I tried: Code: var phone = document.getElementById('phone'); if(isNumericdashes(phone, "Please enter a valid phone number")){ return true; } function isNumericdashes(elem, helperMsg){ var numericExpression = /^[0-9\(\)-]+$/; if(elem.value.match(numericExpression) &&(elem.length > 9)){ return true; }else{ alert(helperMsg); elem.focus(); return false; } } Hi, I have added an validation script for Pizza menu. Validation working fine in all browsers except IE. When I working in IE8 it throws an error "length is null or not an object error in IE8". Please help me. See Java script code below: <script> function UnCheckRadios(radioButtonGroupName) { var formName = "frmOrder"; var form = document.forms[formName]; alert(form); var noOfRadioButtons=form[radioButtonGroupName].length; for(var x=0;x<noOfRadioButtons;x++) { chk=form[radioButtonGroupName] .checked=false; } } </script> PHP code: <input type="radio" id="<?=$subitem['subgroup_id'];?>_comboleft" name="<?=$subitem['subgroup_id'];?>_comboleft" value="<?=$subitem['id'];?>:Left Side" onclick="UnCheckRadios('<?=$subitem["subgroup_id"];?>_combowhole');"> I write code using ext js,openlayers and GeoExt for measuring length and area ,it is working inside inside of india in the world map but this measure tool is not working outside of india in the world map. My code for Measure length: To Load map: var projWGS84 = new OpenLayers.Projection("EPSG:4326"); var projSphericalMercator = new OpenLayers.Projection("EPSG:900913"); var bounds = new OpenLayers.Bounds(68.137, 6.783, 97.336, 37.086); bounds.transform(projWGS84, projSphericalMercator); map.maxExtent = bounds; var gphy = new OpenLayers.Layer.Google("Google Street", { 'sphericalMercator':true //maxExtent: new OpenLayers.Bounds(-20037508, -20037508,20037508, 20037508) } ); var osmLayer = new OpenLayers.Layer.OSM(); // Add the background images via WMS // Add the background images via WMS //map.addLayer(bglayer); // map.addControl(new OpenLayers.Control.MousePosition()); map.addLayers([gphy, osmLayer]); Measure Length: Measure = new Ext.SplitButton({ text:"Measure", iconCls:"icon-measure-length", toggleGroup:"navigate", group: "navigate", enableToggle:true, allowDepress:false, handler: function(button, event) { // allowDepress should deal with this first condition if(!button.pressed) { button.toggle(); } else { button.menu.items.itemAt(activeIndex).setChecked(t rue); } }, listeners: { toggle: function(button, pressed) { // toggleGroup should handle this if(!pressed) { button.menu.items.each(function(i) { i.setChecked(false); }); } }, render: function(button) { // toggleGroup should handle this Ext.ButtonToggleMgr.register(button); } }, menu: new Ext.menu.Menu({ items: [ new Ext.menu.CheckItem( new GeoExt.Action({ text: "Length", toggleGroup:"navigate", iconCls:"icon-measure-length", group: "navigate", enableToggle:true, allowDepress:false, map: map, control: new OpenLayers.Control.Measure(OpenLayers.Handler.Path , { eventListeners: { measu function(evt) { Ext.MessageBox.show({ title: 'Line length :', buttons: Ext.MessageBox.OK, width:200, msg:"Line Length: " + evt.measure.toFixed(2) + " " + evt.units }); } } }) }) ) } }) }) ) ] }) }) How can I call a function in javascript like: Code: var x = string.Length ??? and not like: Code: var x = Length(string); (just for arguments sake) (Also please don't suggest JQuery, I'm doing this to learn!) Thanks! Hi all, I have a free script I have downloaded and I can't figure out how to edit it so that I can change the color of the text to whatever I want it to be and also so that the length of the scrolling text (I am not sure if scrolling is the right term) can be changed. For some reason the text does not go all the way to the end of the page on the right it stops quite a way before. Here is the code: Code: <script language="JavaScript1.2"> <!-- /* Typing Scroller For full source code, usage terms, and 100s more scripts, visit http://dynamicdrive.com */ //Secify scroller contents var line=new Array() line[1]="Testing testing one to three and you can test some more." line[2]="Always testing always testing" line[3]="Trying to test some more and some more again" line[4]="second to last bit of testing." line[5]="Testing text that is just testing one more time" //Specify font size for scoller var ts_fontsize="13px" //--Don't edit below this line var longestmessage=1 for (i=2;i<line.length;i++){ if (line[i].length>line[longestmessage].length) longestmessage=i } //Auto set scroller width var tscroller_width=line[longestmessage].length lines=line.length-1 //--Number of lines //if IE 4+ or NS6 if (document.all||document.getElementById){ document.write('<form name="bannerform">') document.write('<input type="text" name="banner" size="'+tscroller_width+'"') document.write(' style="background-color: '+document.bgColor+'; color: '+document.body.text+'; font-family: arial; font-size: '+ts_fontsize+'; font-weight:bold; border: medium none" onfocus="blur()">') document.write('</form>') } temp="" nextchar=-1; nextline=1; cursor="" function animate(){ if (temp==line[nextline] & temp.length==line[nextline].length & nextline!=lines){ nextline++; nextchar=-1; document.bannerform.banner.value=temp; temp=""; setTimeout("nextstep()",1000)} else if (nextline==lines & temp==line[nextline] & temp.length==line[nextline].length){ nextline=1; nextchar=-1; document.bannerform.banner.value=temp; temp=""; setTimeout("nextstep()",1000)} else{ nextstep()}} function nextstep(){ if (cursor==""){ cursor=""} else if (cursor==""){ cursor=""} else if (cursor==""){ cursor=""} else if (cursor==""){ cursor=""} nextchar++; temp+=line[nextline].charAt(nextchar); document.bannerform.banner.value=temp+cursor setTimeout("animate()",25)} //if IE 4+ or NS6 if (document.all||document.getElementById) window.onload=animate // --> </script> I have been trying to figure this out for about 2 hours but since I am a 100% novice with javascript I have not got very far. Any help would be gratefully recieved. Hey I was just wondering how I could do this... I currently have this Code: <span id="span_1">Title</span> What I want to know is if the span is something like 240px in width how can I make it so if the text inside the span tag is to long and puts the text onto a second line how can i make a cut off point in the text to stop the text moving to a new line and adding '...' if the text is to long?? Hi, I started getting a wierd problem with my jscript program. Here im using jscript for client-side validation and upon email field submission,im getting undefined value with an error saying "length is null or not an object". All the values of fields prior to that are being obtained properly. Please help me out of this crisis I have a string of text, and starting from a particular spot (known to be a number), I want to find the length of that number. Here's what I have: Code: ... var start = [...predetermined...] var end = 0; while (!isNaN(sourceCode.charAt(start + end))){ endCurrent++; } var myNum = sourceCode.substr(start, end) * 1; So let's say var sourceCode = "alkdjabjasdsdf-53 dnalsdb..."; , startCurrent will already be at the "5" and I want to be able to extract the "53". What I have works, but it seems cumbersome... Any advice? Hello, When I run the following code, the .Length function returns "undefined." Please help! This is driving me crazy. Code: var strTest = 'test'; alert(strTest.Length); Using the typeof function, I know that JS is treating the variable as a string. Any suggestions? I am trying to create a Table of Contents from a html document that has h1, h2, and h3 tags. In my js code i retrieve the h3 tags for a given h2 tag and store it into a variable called numSubs then in my for loop i set the condition j < numSubs.length but the problem is that it keeps saying that numSubs.length is 0 so the for loop never gets implemented. Can you tell me why numSubs.length keeps getting set to 0 when it should be 1 or two? Code: var subs = "sub" + (i + 1); var numSubs = document.getElementsByName(subs); alert(numSubs.length); for(j=0; j < numSubs.length; j++) { alert(); toc.innerHTML += "<li>" + numSubs.item(j).innerHTML + "</li>"; } this is the body of the html Code: <h1>Boolean Algebra</h1> <h2>Fundamental Boolean functions</h2> <h3 name = "sub1">Boolean Wrapper Class</h3> <h3 name = "sub1">Boolean Computations</h3> <h2>Computational Completeness</h2> <h3 name = "sub2">Calculating Machanics</h3> <h2>Identities of Boolean algebra</h2> <h3 name = "sub3">NAND and NOR</h3> <h2>NAND and NOR</h2> I need to have an online submission form, which accepts text for print adverts. Here is my problem: Whereas with Twitter, you are limited to 140 characters as the output is digital, with print the overall width of text is vital if it has to fit within a given space. Are there any javascript software libraries available that can calculate the printed length? Preferably as the user types. This assumes that the font, font-size and kerning are known. (I do not want to use the 'Courier' font) We will have to write this in-house unless there is something available on the internets (I'm possibly not the first person to come up against this) Can anyone help? (I've posted this at 'graphicdesignforum' but figure the readership will be different) |