[英]i have an array and i need to change an array format using javascript
[英]need javascript array format
我正在尋找在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' }
]
}
]
你去。
在相關說明中, JSON可能值得一看。 盡管我必須說,以上不是json,而只是json樣。
在Java或C#之類的強類型語言中,這將很棘手,但是在JavaScript中這很容易。
由於JavaScript在數組值和變量方面沒有強類型,因此您只能創建對象文字數組。 每個對象將包含您指定的屬性。 無需在每個屬性上指定string
類型-JavaScript會為您推斷出來。
盡管這確實很容易做到,但是缺點是您不會進行任何類型檢查,因此某些代碼可能會無意間將對象粘貼到您的字符串字段之一中,而JavaScript不會阻止它。
因此,請注意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.