Javascript 如何以数组中对象的内容为目标?

Javascript 如何以数组中对象的内容为目标?,javascript,jquery,Javascript,Jquery,我试图将数组中每个对象中的特定项作为目标,以便在其他地方将它们用作变量。如何使用每个项目创建变量?类似var thisThing=things.title things: [ {title: "The title for this thing", url: "https://stackoverflow.com"}, {title: "The title for this thing", url: "https://stackoverflow.com"}, {title:

我试图将数组中每个对象中的特定项作为目标,以便在其他地方将它们用作变量。如何使用每个项目创建变量?类似var thisThing=things.title

things: [
    {title: "The title for this thing", url: "https://stackoverflow.com"},
    {title: "The title for this thing", url: "https://stackoverflow.com"},
    {title: "The title for this thing", url: "https://stackoverflow.com"},
    {title: "The title for this thing", url: "https://stackoverflow.com"}
    ]

您可以使用ES6来实现这一点。只需将变量名用大括号括起来,使其与对象属性的名称匹配即可。例如:

const things=[{
标题:“这件事的标题1”,
url:“https://stackoverflow.com"
},
{
标题:“这件事的标题2”,
url:“https://stackoverflow.com"
},
{
标题:“这件事的标题3”,
url:“https://stackoverflow.com"
},
{
标题:“这件事的标题4”,
url:“https://stackoverflow.com"
}
];
things.forEach(thing=>{
const{title,url}=thing;
控制台日志(标题);
console.log(url);
控制台日志(“”);

})
您可以通过
事物[0]获取第一个事物的标题。标题
,第二个通过
事物[1]。标题
等。有关详细信息,请参阅链接问题的答案。您还可以创建数组。