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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18831817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:21:50  来源:igfitidea点击:

Array of 'associative array' in (node)js

javascriptphpnode.js

提问by DrakaSAN

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

我在 php 中有这段代码可以在 js 中翻译(准确地说是节点)

$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.

目标是,如果我执行 console.log(config.test.['something']);,则在输出中包含链接。

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

有没有办法在没有服务器的情况下测试它(因为我明天之前没有),或者我的语法好吗?

回答by NDM

Javascript does not have associative arrays, only plain objects:

Javascript 没有关联数组,只有普通对象:

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

用于将 PHP 数组转换为 javascript 使用 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

回答by letiagoalves

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.

没有太多要测试的,但如果您愿意,可以使用 Firebug 或 Chrome 开发人员工具等工具。

回答by srain

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

使用chrome浏览器,默认打开控制台,快捷键是F12。您可以在控制台中测试您的代码。