简体   繁体   English

您如何通过键从 JavaScript 字典数组中获取 object?

[英]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.

相关问题 在JavaScript中,如何从作为外部对象中数组元素的对象的方法到达外部对象的“ this”对象 - In JavaScript, how do you get to the 'this' object of the outer object, from a method of an object that's an element of an array in the outer object 如何从 object 中获取 object.key 数组(不是值,不是字符串,不仅是键)? - How do you get an array of object.key(not values, not string, not only keys) from an object? 在Javascript中,如何从混合数组中的对象访问键值数据? - In Javascript, how do you access key value data from an object in a mixed array? 你如何在javascript映射对象中获取特定索引处的键? - How do you get the key at specifc index in javascript map object? 如何获得不可变 object 的密钥? - How do you get the key for an Immutable object? Javascript 如何从数组 Object 中分别获取值和键? - Javascript how to get value and key seperately from Array Object? 如何从数组中的单个对象获取键? - How do I get a key from a single object in the array? 如何在JavaScript中获得对象的名称? - How do you get the name of an object in JavaScript? 我如何使用javascript中具有相同键的对象从对象中创建具有对象数组的对象数组 - how do I create an array of objects with array in the object from an object with the same key in javascript 如何从 Javascript 中的嵌套对象获取值数组 - How do I get array of values from a nested object in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM