86092

creating a multidimensional array (nXn) matrix using javascript

Question:

I am trying to create a matrix table. The values to be plugged into the matrix will be user input from an html table (basically a unique pairing table which pairs two elements at a time and for each pair a user can select which he prefers and by how much it is prefered on a scale of 1-9 using radio buttons). it is the preferences i.e. 1-9 that gets plugged into the matrix table and the number of unique pairing that detemines the lenght of the matrix.

when the matric is generated i want to have something like this:

0 1 2 3 4 sum 1 [1] [6] [2] [8] 17 2 [ ] [1] [9] [4] 15 3 [ ] [ ] [1] [7] 8 4 [ ] [ ] [ ] [1] 1

The problem is now i want to get the values of clicked radios into the matix. I know how to get it but just don't know how to fit it into the matrix. Here is the code I have for now, pls ask me questions if needed thanks:

$(function(){ var tableLenght = new Array(); $('#matrix').click(function(){ var counter = 0; $("#riskForm tr").each(function(){ //ignores the header of the table (the title) if(counter >=1){ tableLenght.push($(this).closest('tr').children("td:eq(1)").text()); } counter++; }); // get unique attributes of in our table var uniqueList = new Array(); //push first element onto list uniqueList.push(tableLenght[0]); //pushes current elem onto the array var cur = tableLenght[0]; //sets first element by default for(var i = 1; i < tableLenght.length; i++){ //loops through the whole lenght of our array if(cur != tableLenght[i]){//checks if the current is not same as the next one cur = tableLenght[i]; //sets the new elemt uniqueList.push(tableLenght[i]); //pushes onto the array } } alert(uniqueList); //alerts only the unique table rows in the tableLenght array var myArray = new Array(uniqueList); for (var i = 0; i < uniqueList; i++) { myArray[i] = new Array(uniqueList); for (var j = 0; j < uniqueList; j++) { myArray[i][j] = ''; } } }); //to show/get the values of selected radio buttons function showValues() { var fields = $( ":input").serializeArray(); $( "#results" ).empty(); jQuery.each( fields, function( i, field ) { $( "#results" ).append( field.value + " " ); }); } $( ":checkbox, :radio" ).click( showValues ); showValues(); $('#display').click(function (n) { document.location.href="trialIndex.php" }); });

Your help is much appreciated!

Answer1:

I think you are trying to implement the AHP algorithm here. It is quite complex to write the whole code here and how it'll work but here is a link of an already exiting work on AHP. Hope it helps: <a href="https://github.com/humbertoroa/ahp" rel="nofollow">https://github.com/humbertoroa/ahp</a>

Answer2:

You can push a value to an array, for example:

var a = []; a.push(1); a.push(2); console.log(a); // [1, 2]

You can set it based on index:

var a = []; a[0] = 1; a[1] = 2; console.log(a); // [1, 2]

To create a multi dimensional array you can simply push arrays to one array.

var a = [1,2,3]; var b = [4,5,6]; var c = []; c.push(a); c.push(b); console.log(c); // [[1, 2, 3], [4, 5, 6]]

Recommend

  • add text input to inline radio buttons
  • Generic way to get the value of an input regardless of type
  • Tips for conserving battery if CoreLocation must constantly retrieve the user's location?
  • Enable/disable a form element after checking radio button checked states
  • Changing Radio Buttons image, help
  • Cant get $_POST values from multiple forms
  • Reverse query for Hierarchical Data
  • Sails.js associations
  • How do I get narrow width and a large height on a RadioButtons widget, and still have round radio bu
  • TimePickerDialog widget in landscape mode (PreferenceScreen)
  • R script - least squares solution to the following [duplicate]
  • Height of tableView's header
  • got OSError when use pillow to deal with my jpg image
  • Displaying and sizing a grayscale from a QImage in Qt
  • When i use auto bi = 123456789, in C++, is it always assigned as an int?
  • How to use a decaying learning rate with an estimator in tensorflow?
  • adding item to window Extjs 4
  • Why are my web pages zoomed in when I open them in Opera Mobile?
  • How to resize image if the image is bigger than Textbox.But it not increase scale if smaller
  • How to merge keras sequential models with same input?
  • Prevent page break in text block with iText, XMLWorker
  • Can I read an iPhone beacon with Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData
  • How VBA declared Volatility works
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • How to handle AllServersUnavailable Exception
  • jquery mobile loadPage not working
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Data Validation Drop Down Box Arrow Disappearing
  • How to delete a row from a dynamic generate table using jquery?
  • Benchmarking RAM performance - UWP and C#
  • using HTMLImports.whenReady not working in chrome
  • How do you join a server to an Active Directory (domain)?
  • Angular 2 constructor injection vs direct access
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?