[英]How do you get an object from an array of JavaScript dicts by key?
I hope I'm using the right terminology...I'm usually a Python guy, so I'm used to the dictionary datatype.我希望我使用的是正确的术语......我通常是一个 Python 的人,所以我习惯了字典数据类型。 Anyway, I'm working on a Node app right now, and I've run into a problem.
不管怎样,我现在正在开发一个 Node 应用程序,但遇到了一个问题。 I'm having users select from a dropdown menu, which is populated via an array of dictionary objects passed into EJS.
我有一个下拉菜单中的用户 select,该菜单是通过传递给 EJS 的字典对象数组填充的。 Each object has a slug, and a name.
每个 object 都有一个 slug 和一个名字。 So, it would be something like this:
所以,它会是这样的:
const computers = [{slug: 'dell-desktop', name: 'Dell Desktop'}, {slug: 'macbook-pro', name: 'MacBook Pro'}, {slug: 'imac-27-inch', name: 'iMac, 27-Inch'}];
I populate the form using the names, but I will need the slugs for URL stuff in the backend.我使用名称填充表单,但我需要在后端使用 URL 的 slug。 So it looks like this:
所以它看起来像这样:
app.get('/form', function(req, res){
res.render('form.ejs', {computers: computers});
});
Everything seems to work beautifully in general, with the form populating as expected:总体而言,一切似乎都运行良好,表单按预期填充:
<select id = 'computer' name = 'computer'>
<% for (var i = 0; i<computers.length; i++) { %>
<option value = '<%= computers[i].slug %>'><%= computers[i].name %></option>
<% } %>
</select>
However, the data that I receive from the form just provides the slug.但是,我从表单收到的数据只是提供了 slug。 My question is, is there a good way to use the slug key to then get the name from the array of dictionaries?
我的问题是,有没有一种好的方法可以使用 slug 键从字典数组中获取名称? I've tried like this, but it's not working, and I wonder if it's because they're in an array?
我这样试过,还是不行,不知道是不是因为他们在一个数组里?
app.post('/form', function(req, res){
var c_slug = req.body.computer;
var name = computers[c_slug];
console.log(c_slug);
console.log(name);
res.send('Success!')
});
I haven't been able to find anything specific to this in my searches.我无法在搜索中找到与此相关的任何内容。 I've found a mapping explanation, but that gets all of the values for a given key.
我找到了一个映射解释,但是它获取了给定键的所有值。 I'm hoping there's just something obvious I'm missing!
我希望我缺少一些明显的东西!
You can use Array.find() method to find the element in array which matches given criteria.您可以使用Array.find()方法在数组中查找符合给定条件的元素。
It returns the value of the first element in the array that satisfies the provided testing function. Otherwise,
undefined
is returned.它返回满足提供的测试 function 的数组中第一个元素的值。否则,返回
undefined
。
app.post('/form', function(req, res){
const c_slug = req.body.computers;
const element = computers.find(el => el.slug === c_slug);
// element is either an element that was found or undefined
if (element) {
const name = element.name;
// the above can be written like this in ES6
// const { name } = element;
// do something
} else {
// return 400 (Bad Request)
}
...
PS I'm using const
instead of var
which is ES6 syntax and arrow function () =>...
. PS我使用的是
const
而不是var
,它是ES6语法和箭头 function () =>...
Both are available on server-side in NodeJS 8+.两者都可以在 NodeJS 8+ 的服务器端使用。
you can use.find() method你可以使用.find() 方法
app.post('/form', function(req, res){
var c_slug = req.body.computers;
var computer = computers.find(el => el.slug === c_slug);
console.log(c_slug);
console.log(computer.name);
res.send('Success!')
});
or you can find it manually或者你可以手动找到它
app.post('/form', function(req, res){
var c_slug = req.body.computers;
var name ;
for (let i = 0; i < computers.length; i++) {
if (computers[i]['slug'] === c_slug) {
name = computers[i]['slug'];
break;
}
}
console.log(c_slug);
console.log(name);
res.send('Success!')
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.