views:

72

answers:

2

Hoping someone can assist and hoping this is possible in JavaScript. I basically have the following string in the format:

A,B,C:D,E,F

What I am trying to achieve is a means of pairing up left hand side JavaScript variable, to the left of the ":" with the right hand side values to the right of the ":"

I basically would like to have the following variables set in , so that I can use in my coding, i.e:

var A = D;
var B = E;
var C = F;

I can then use the values of A,B and C as parameters into other JavaScript functions.

I have looked at the split and slice methods for this string manipulation but unsure how to pair up left hand side with right hand side values.

Any help would be great, thanks!

A: 

The pairup function here generates the code to do the pairing. You'd need to eval the returned code to actually execute it.

<script>

function pairup(s) {
  s = s.split(":");
  var lhs = s[0].split(",");
  var rhs = s[1].split(",");
  var pairing = "";
  for (var i = 0; i < lhs.length; i++) {
    pairing += "var " + lhs[i] + " = " + rhs[i] + "; \n";
  }
  return pairing;
}

alert(pairup("A,B,C:D,E,F"));

</script>

If you have the objects ready to bind the values of the variables too, then you can do something like this:

<script>

function pairup(s, oleft, oright) {
  s = s.split(":");
  var lhs = s[0].split(",");
  var rhs = s[1].split(",");
  for (var i = 0; i < lhs.length; i++) {
    oleft[lhs[i]] = oright[rhs[i]];
  }
}

oleft = {};
oright = { firstName: "F", lastName: "L" };
pairup("givenName,familyName:firstName,lastName", oleft, oright);
alert(oleft.givenName + ", " + oleft.familyName);

</script>
polygenelubricants
+3  A: 

With this method (attaching variables to an object), you can reference variables in code without an eval statement.

a = "A,B,C:D,E,F";
array = a.split(":"); //split on the colon, get two strings
lefts = array[0];
rights = array[1];

obj = {} //object to attach variables to.
for( var i = 0; i < lefts.length; i++ )
{
  obj[lefts[i]] = rights[i]; //set the member variables of obj
}

obj.A // D
obj.B // E
obj.C // F

If you care about the wasted comma property, check if lefts[i] is equal to a comma before you set on the object.

Stefan Kendall
Or obj['x'] when x is an invalid (or even valid) identifier.
trinithis
Well... This does what it says, but also creates a "," property on `obj` (with a value of... ",").
Shog9
This uses the **strings** "D", "E", and "F" as the right hand side. It doesn't actually assign the values of the **variables named** D, E and F, to the variables named A, B, C. What is it that the original poster wants?
polygenelubricants
@polygenelubricants: **that** is an excellent question...
Shog9
This also doesn't work when `a = "A1,A2,A3:B1,B2,B3";`.
polygenelubricants
My reading of the question yielded my implementation. It may or may not be what the OP was looking for, but it's what I read he's *asking* for.
Stefan Kendall
sorry for the confusion, just to clarify, D,E and F here are actual values I want to assign to the left hand side variables.
tonsils
Then my solution would work if you don't mind one that generates a code that you need to eval. If otherwise you already have an object whose fields you want to pair up, then you can modify it to do just that.
polygenelubricants
polygenelubricants, can you pls explain the eval side of things pls. What I basically want to achieve is to the pass the values assigned to A,B and C into other function calls.
tonsils
@tonsils: try out my answer and see if it does what you need. I can help if you need further clarification.
polygenelubricants
polygenelubricants, that worked great but just realised that the value I currently have assigned to F is in the format of ":VAR1,VAL1" and b/c of split on ":" and ",", this is causing issues. Is there anyway to ignore after first ":" - Thanks.
tonsils
polygenelubricant - also, now that I need to use the actual individual variables in other code, so can I just issue the command eval(pairup("A,B,C:D,E,F")); - is this correct? I need to access the individual variables.
tonsils
The value of F itself shouldn't matter. Are you saying that the pairing formula string itself (`a` in your example) can contain multiple `:` and the left hand side and the right hand side may not have the same number of `,`?
polygenelubricants
actually, I can change the format of this so this is not an issue. I am now unsure how to evaluate and say access variable A alone - can you pls assist.
tonsils
yes, that's what I mean by `eval` is that you can just take the returned string and just evaluate it. Returning the string also gives you a chance to figure out what it does and how it works if you need to modify it for your own need.
polygenelubricants
After you do `eval(pairup("A,B,C:D,E,F"))`, you can access `A` just as you would any other variable. I guess I'm not sure what your problem is.
polygenelubricants
Oh, so I can simply just do a alert(a); or pass a into another function - is this correct? I also assume that pairing is a global variable?
tonsils
seems like it doesn't like the eval function call. I am getting an invalid character error - any ideas pls?
tonsils
The code for `pairup` that I gave you declares `pairing` as a local variable. You can make it global, but I'm not sure why that'd be necessary.
polygenelubricants
The code for `pairup` I gave you uses uppercase V because that's what you used in your original question. Javascript is case-sensitive, so you'd want to use lowercase v instead if you're doing the `eval` approach.
polygenelubricants
Actually, changed that to lowercase var and added to the return part of your function return eval("pairing") but when I try to display the value of A, it's says that it's undefined.
tonsils
If you want to evaluate inside `pairup`, then you just `eval(pairing);`, not `eval("pairing");`.
polygenelubricants
also tried your solution Stefan and also getting an undefined error.
tonsils
This is not how the commenting system was meant to be used. I had 23 notifications this morning.
Stefan Kendall