-2

can I use following (PHP) Syntax to create an array in JavaScript?

$arr = ("foo" => "bar", "foobar" => "test");

I dont want to use following syntax (in JS):

var arr = [];
arr["foo"] = "bar";
arr["foobar"] = "test";

Thank you very much!

1
  • 2
    Those aren't arrays, those are objects. Commented Oct 22, 2014 at 8:27

1 Answer 1

1
var arr = {foo:"bar", foobar:"test"};

var foo = arr.foo;
var foobar = arr.foobar;
Sign up to request clarification or add additional context in comments.

6 Comments

You really shouldn't answer questions as basic as this one. Stack Overflow is not a JS api.
Thanks! The reason why I dont use objecs, is, that I cant use a variable as a key. Array: arr[key] (key is a variable).
@Tream The first thing that comes into mind is that you can use eval() vor variables: eval('arr.' + key)
Dude, no. Don't ever use eval. Especially not like that! Use arr[key] instead.
You can also use arr[key] for objects. See stackoverflow.com/questions/2241875/…
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.