我有一个由php生成的javascript数组,这使得:
var sections = [
section1 =>
[name => anthony, email => test@test.com],
section1 =>
[name => anthony, email => test@test.com],
section1 =>
[name => anthony, email => test@test.com],
section2 =>
[name => anthony, email => test@test.com],
section2 =>
[name => anthony, email => test@test.com]
]
最好的方法是什么:
var sections = [
section1 =>
[name => anthony, email => test@test.com],
[name => anthony, email => test@test.com],
[name => anthony, email => test@test.com],
section2 =>
[name => anthony, email => test@test.com],
[name => anthony, email => test@test.com]
]
合并所有类似的键并将所有内部数组放入同一个池中。
所以我可以将每个部分作为" Section"而不是同一部分。
答案 0 :(得分:0)
您发布的内容不是有效的JavaScript,因此您所寻找的结构并不明显。此外,您不应该让PHP输出有问题/重复JavaScript,然后尝试在JavaScript中稍后进行清理,应该修改PHP以生成良好的JavaScript。将输出存储在PHP数组中而不是仅仅输出它然后只将唯一元素(节)输出到JavaScript
应该不难话虽这么说,但是你现在必须对输出做一个简单的修改才能使它成为有效的JavaScript。我继续将"1"
,"2"
等附加到字符串的末尾,以明确重复的内容:
var sections = {
section1 :
{name : "anthony1", email : "test@test.com1"},
section1 :
{name : "anthony2", email : "test@test.com2"},
section1 :
{name : "anthony3", email : "test@test.com3"},
section2 :
{name : "anthony4", email : "test@test.com4"},
section2 :
{name : "anthony5", email : "test@test.com5"},
};
console.log(sections.section1.name, sections.section1.email); // "anthony3" "test@test.com3"
console.log(sections.section2.name, sections.section2.email); // "anthony5" "test@test.com5"
没有附加的"1"
,"2"
等:
var sections = {
section1 :
{name : "anthony", email : "test@test.com"},
section1 :
{name : "anthony", email : "test@test.com"},
section1 :
{name : "anthony", email : "test@test.com"},
section2 :
{name : "anthony", email : "test@test.com"},
section2 :
{name : "anthony", email : "test@test.com"},
};
console.log(sections.section1.name, sections.section1.email); // "anthony" "test@test.com"
console.log(sections.section2.name, sections.section2.email); // "anthony" "test@test.com"