简体   繁体   English

如何在JavaScript中选择所选对象

[英]how to select the chosen object in JavaScript

This is my JavaScript object: 这是我的JavaScript对象:

   {
    "login" : {
        "title" : "title1"
        "content1" : "content1"
        }
    "Menu" : {
        "title" : "title2"  
        }
    }

How do I get the value of of a property of the JavaScript object with function getvalue("login","title") or getvalue("login","content1") and return title1 or content1 ? 如何使用函数getvalue("login","title")getvalue("login","content1") getvalue("login","title")获得JavaScript对象的属性值并返回title1content1 I do this to make a multi-language phonegap apps so when the function called i compare with lang attribute in html, i use xui and pure javascript and no jquery becouse it's to heavy for phonegap i think.. can anyone help me? 我这样做是为了制作一个多语言的phonegap应用程序,所以当我调用的函数与html中的lang属性进行比较时,我使用了xui和纯javascript,并且没有jquery,因为它对phonegap来说太沉重了。有人可以帮我吗? or is there any best method for multilanguage? 还是有什么最好的多语言方法?

Your JSON 您的JSON

myJSON = { 
  'login' : { 'title' : 'title1'},
  'menu' : { 'title' : 'title2'} 
};

You can call the value like this: 您可以像这样调用值:

alert(myJSON.login.title);

it will output: title1. 它将输出:title1。

So assuming this is a standard json in a standard variable: var myJson. 因此,假设这是标准变量中的标准json:var myJson。 Why cant you return 你为什么不能回来

myJson.login.title (per this syntax) myJson.login.title (使用此语法)

In the example above login is not an array so you wouldnt be able to return a list of login values 在上面的示例中,login不是数组,因此您将无法返回登录值列表

With regards to internationalization I would do something simpler (and I have been doing that successfully in my apps). 关于国际化,我会做一些简单的事情(并且我已经在我的应用程序中成功地做到了这一点)。 Just create a set of name-value pairs in a file calls language.en.xml (or json etc depending if you can easily parse json or xml using your framework) and one called language.it.xml if lets say you wanted to have the italian language as well. 只需在一个名为language.en.xml(或json等,取决于您是否可以使用框架轻松解析json或xml)的文件中创建一组名称-值对,以及一个称为language.it.xml的文件(如果假设您希望拥有意大利语也是如此。 Then each key (eg login) is the same for every file and the value is the title you want to have in each language. 然后,每个文件的每个键(例如,登录名)都相同,并且值是您想要使用每种语言显示的标题。 So pretty similar to yours but you dont need a hierarchy. 非常类似于您的,但是您不需要层次结构。

Then you can create a static method called Internationalize('Login') that will return the value of login for the language the user has chosen. 然后,您可以创建一个称为Internationalize('Login')的静态方法,该方法将返回用户选择的语言的login值。 So in your method you will check the users language settings and read the value from the required file. 因此,在您的方法中,您将检查用户语言设置并从所需文件中读取值。 If language is IT then check value in language.it.xml (or json etc.) if default or NULL or en then check the english file. 如果语言是IT,则检查language.it.xml(或json等)中的值(如果默认)或NULL,或者检查英语文件。

Optimizations like pre-loading the language file contents in memory are also things you should be thinking about. 诸如将语言文件内容预加载到内存中之类的优化也是您应该考虑的事情。

i found the answer finally, here it is : 我终于找到了答案,这是:

var data = {
"login" : {
    "title" : "title1"
    "content1" : "content1"
    }
"Menu" : {
    "title" : "title2"  
    }
}

function getvalue(resource, paramA, ParamB) 
{
   return resource[paramA][paramB];
}

end then just called : 结束然后只是调用:

getvalue(data,"login","content1")

it will return content1 它将返回content1

huff, finally i found this :) ,终于我找到了这个:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM