(节点)js中的“关联数组”数组

(节点)js中的“关联数组”数组

问题描述:

I have this code in php to translate in js (node to be precise)

$config['test'] = array(
     "something" => "http://something.com/web/stats_data2.php"
    ,"somethingelse" =>"http://somethingelse.com/web/stats_data2.php"
    ,"anothersomething" =>"http://anothersomething.com/web/stats_data2.php"
);

So I started to write this:

config.test = [
      something = 'http://something.com/web/stats_data2.php'
    , somethingelse = 'http://somethingelse.com/web/stats_data2.php'
    , anothersomething = 'http://anothersomething.com/web/stats_data2.php']

But I m not sure if it s not supposed to be writed like this instead:

config.test.something = 'http://something.com/web/stats_data2.php';
config.test.something = 'http://somethingelse.com/web/stats_data2.php';
config.test.anothersomething = 'http://anothersomething.com/web/stats_data2.php';

The goal is, if I do console.log(config.test.['something']);, to have the link in the output.

Is there any way to test it without server (since I don t have any before tomorrow), or is my syntax good?

我在php中使用此代码在js中转换(节点是精确的) p>

  $ config ['test'] = array(
“something”=>“http://something.com/web/stats_data2.php"
,”somethingelse“=>”http  ://somethingelse.com/web/stats_data2.php“
,”anothersomething“=>”http://anothersomething.com/web/stats_data2.php“
); 
  code>  pre  > 
 
 

所以我开始写这个: p>

  config.test = [
 something ='http://something.com/web/stats_data2  .php'
,somethingelse ='http://somethingelse.com/web/stats_data2.php'
,anothersomething ='http://anothersomething.com/web/stats_data2.php'] 
  code>   pre> 
 
 

但我不确定它是否应该这样写: p>

  config.test.something ='http  ://something.com/web/stats_data2.php'; 
config.test.something ='http://somethingelse.com/web/stats_data2.php';
config.test.anothersomething ='http:// anothersomething  .COM /网络/ stats_data2  .php'; 
  code>  pre> 
 
 

目标是,如果我执行console.log(config.test。['something']);,以获取链接 输出。 p>

有没有办法在没有服务器的情况下测试它(因为我明天之前没有),或者我的语法是否合适? p> div>

Javascript does not have associative arrays, only plain objects:

var myObj = {
    myProp: 'test',
    mySecondProp: 'tester'
};

alert(myObj['myProp']); // alerts 'test'

myObj.myThirdProp = 'testing'; // still works

for (var i in myObj) {
    if (!myObj.hasOwnProperty(i)) continue; // safety!
    alert(myObj[i]);
}
// will alert all 3 the props

For converting PHP arrays to javascript use json_encode

If you want to play it safe though, you would want to quote the properties as well, as reserved keywords will make your construct fail in some browsers, or will not be accepted by some compression systems:

var obj1 = {
    function: 'boss',       // unsafe
    'function': 'employee'  // safe
};

console.log(obj1.function);    // unsafe
console.log(obj1['function']); // safe

Simply create a generic object with your configurations:

var config = {
   test: {
      something: 'http://something.com/web/stats_data2.php',
      anothersomething: 'http://anothersomething.com/web/stats_data2.php'
   }
};

Then you can use it this way:

var something = config.test.something;

or

var something = config.test['something'];

or

var something = config['test']['something'];

There is not so much to test but if you want to you can use tools like Firebug or Chrome Developer Tools.

Using chrome browser, open console default, the short cut key is F12. You can test your code in the console.