简体   繁体   English

需要JavaScript数组格式

[英]need javascript array format

I am looking to create following format in array in javascript. 我正在寻找在javascript中的数组中创建以下格式。

Each item has: 每个项目都有:

  • url -> string (which is login URL) url->字符串(这是登录URL)
  • name -> string 名称->字符串
  • inputs -> array 输入->数组

And each input is: 每个输入是:

  • name -> string 名称->字符串
  • type -> string 类型->字符串
  • value -> string 值->字符串

Please guide me , how to do this in JavaScript 请指导我,如何在JavaScript中执行此操作

[
   {url: 'http://google.com/',
    name: 'google',
    inputs: [
        { name: 'search-term', type: 'string', value: 'javascript' },
        { name: 'region', type: 'country-code', value: 'IN' }
    ]
   },
   {url: 'http://yahoo.com/',
    name: 'yahoo',
    inputs: [
        { name: 'search-term', type: 'string', value: 'javascript' },
        { name: 'region', type: 'country-code', value: 'US' }
    ]
   }
]

there you go. 你去。

on a related note, JSON might be worth looking at once. 在相关说明中, JSON可能值得一看。 Though I must say, the above is not json, only json-like. 尽管我必须说,以上不是json,而只是json样。

This would be tricky in a strongly typed language like Java or C#, but it's pretty easy in JavaScript. 在Java或C#之类的强类型语言中,这将很棘手,但是在JavaScript中这很容易。

Since JavaScript doesn't have strong typing for array values and variables, you can just create an array of object literals. 由于JavaScript在数组值和变量方面没有强类型,因此您只能创建对象文字数组。 Each object would contain the properties you specified. 每个对象将包含您指定的属性。 There's no need to specify the string type on each property--JavaScript will infer that for you. 无需在每个属性上指定string类型-JavaScript会为您推断出来。

While this is really easy to do, the drawback is that you're not going to get any type checking, so some code could inadvertently stick an object into one of your string fields and JavaScript won't stop it. 尽管这确实很容易做到,但是缺点是您不会进行任何类型检查,因此某些代码可能会无意间将对象粘贴到您的字符串字段之一中,而JavaScript不会阻止它。

So just beware of the advantages and disadvantages of JavaScript's flexibility, and make sure you're doing server-side sanity checks on your data. 因此,请注意JavaScript灵活性的优点和缺点,并确保对数据进行服务器端的完整性检查。

var items = 
[
    {
        url: "http://...",
        name: "FOO",
        inputs: [
            {
                name: "Input1",
                type: "LeTypeh"
                value: "Levalueh"
            },
            {
                name: "Input2 (Foo)",
                type: "LeType2a",
                value: "Levalue2j"
            }
            // ... (insert as many comma separated inputs as you need in the foo item
    },
    {
        url: "http://...",
        name: "BAR",
        inputs: [
            {
                name: "Input1",
                type: "LeTypeh"
                value: "Levalueh"
            },
            {
                name: "Input2 (Bar)",
                type: "LeType2a",
                value: "Levalue2j"
            }
            // ... (insert as many comma separated inputs as you need in the bar item here
    }
    //... (insert as many comma separated items as you need in the array here
]

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

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