Okay, so I've sorted a numerical array and the minimum is at the top of the heap with [0]. How do I keep track of the original [?] so that I can access the associated parameters?
From which array. The original order or the sorted order? The more explicit you are in what you are trying to do, the easier it is to help you.
And if you want the car name it would be arr[1][1] not arr[1][0].
I want the association which existed in the original array.
Disregard stupid question below. Values must be assigned before the next array is declared. This looks promising!
Here's something...from one of the many javascript help sites...which would, I believe, solve my problem (if I can get it to work).
var cube = new Array();
cube[0] = new Array(); // This gives us a two dimensional array
cube[0][0] = new Array(); // This gives us a three dimensional array
cube[0][0][3] = "Hello";
// This is how we use a three dimensional array
and document.write(cube[0][0][3]) yields "Hello."
But, as soon as I assign a value in the second array:
var cube = new Array();
cube[0] = new Array(); // This gives us a two dimensional array
cube[0][0] = new Array(); // This gives us a three dimensional array
cube[0][0]=2;
cube[0][0][3] = "Hello";
// This is how we use a three dimensional array
document.write(cube[0][0][3]) yields "undefined."
Doesn't seem to make sense. What good is a multidimensional array if you can only use the last array?
Last edited by BillyShope; 07-09-2010 at 07:33 AM.
Reason: added "Disregard" paragraph.
What you are trying is making no sense. You assign a value to cube[0][0] and then you pretend like it's an array.
Code:
cube[0][0] = new Array(); // This gives us a three dimensional array
cube[0][0]=2; // Which you overwrite
Without knowing what problem you are trying to solve, it's impossible to help you. In general, it's far better to just outline your problem rather than asking how to code a specific solution.
Great wit and madness are near allied, and fine a line their bounds divide.
What you are trying is making no sense. You assign a value to cube[0][0] and then you pretend like it's an array.
Code:
cube[0][0] = new Array(); // This gives us a three dimensional array
cube[0][0]=2; // Which you overwrite
Without knowing what problem you are trying to solve, it's impossible to help you. In general, it's far better to just outline your problem rather than asking how to code a specific solution.
Yes, I realized later I was making that mistake.
Okay, here's my problem: I'm working with a mechanism that has hundreds of different linkage arrangements. I have a target number and can calculate, for each linkage arrangement, just how far I am from the target. I want to give the user the 5 best linkage arrangements, or, the 5 linkage arrangements which deviate the least from the target. So, once I've sorted, I need to recall the linkage arrangements for each of the 5.
var arr = ["Chevrolet","Ford","Mazda","Toyota"];
var newarr = [[],[],[],[],[]];
var str = ["2,1,3,4","","","",""];
for (var i = 0; i < str.length; i++) {
var temp = str[i].split(",");
for (var j = 0; j < str[i].length; j++) {
newarr[i][j] = arr[temp[j]-1];
}
}
// newarr[0] = ["Ford","Chevrolet","Mazda","Toyata"];
// arr = original one
I think that's more suitable?
Great wit and madness are near allied, and fine a line their bounds divide.
Yours was indeed the solution to my initial problem, but it introduced a couple of other problems.
First, I didn't immediately realize that I was...with your solution...sorting a string variable. I'll be working with numbers. I suppose I could multiply by a constant, round, and convert to a string, but that wouldn't help me with the second problem.
As mentioned earlier, I won't be working with 4 cases, but with hundreds. I would prefer inputting to a simple numeric array with each iteration.
Sorry to keep coming back, but I've put a lot of work into this spreadsheet...one of many which I provide free to hundreds of clients..., but it's of no value if I can't output the results.
Thank you.
Last edited by BillyShope; 07-10-2010 at 06:24 AM.
Reason: added "...with your solution..."
How do you want the data to be inputted? Where are these lists coming from? I presumed a comma separated list for some reason, but an array is almost the exact same, because I converted that list to an array anyway.
Code:
var str = [[2,1,3,4],[4,3,2,1],[],[],[]];
for (var i = 0; i < str.length; i++) {
// var temp = str[i].split(","); No need for this line anymore
And I think I mentioned that [] is just shorthand for an array.
Code:
var arr = new Array();
arr[0] = 1;
arr[1] = 2;
Is exactly the same as this.
Code:
var arr = [1,2];
And don't worry too much about typecasting in JavaScript. It will do it automatically for you most of the time. The only time you really have to worry is when adding two numbers in string format, because concatenation operator is the same as addition.
Code:
alert("1"+"2"); // = 12
alert(1 + 2); // = 3
Great wit and madness are near allied, and fine a line their bounds divide.
I believe I've figured it out. And, when you see my solution, I'm sure you'll be saying, "Well, if that's all you needed." I know so little about Javascript that I'm embarrassed to come to a forum like this, but I was desperate. Sorry I couldn't explain my problem more accurately.
Bookmarks