从数组中的所有对象获取特定键的值

时间:2021-07-20 21:14:10

I'm running an express.js app that has a few apis feeding data to dropdown boxes. The data returned is in the form:

我正在运行一个express.js应用程序,它有几个api向下拉框提供数据。返回的数据采用以下形式:

  [
    {
        key: 'blah',
        value: 'Blah Blah'
    },
    {
        key: 'foo',
        value: 'Foos'
    },
    {
        key: 'bar',
        value: 'Bars'
    },
    {
        key: 'baz',
        value: 'Bazingo'
    }
];

where key is my option key and value is the display text. The structure of this array is fixed and I know for a fact that I'll always have key and value as the fields in each object in the array.

其中key是我的选项键,value是显示文本。这个数组的结构是固定的,我知道我将始终拥有键和值作为数组中每个对象的字段。

When I try to validate the form submitted (additional server side validation), I'd like to cross-reference the value provided for a field against all the values for "key" in the array (blah, foo, bar, baz). Given that this is going to be a frequently used route, I'd like to avoid iterating over the array to find the permitted values, every single time. Is there a simpler way to do this? In other words, I know I can use:

当我尝试验证提交的表单(额外的服务器端验证)时,我想交叉引用为字段提供的值与数组中的“key”的所有值(blah,foo,bar,baz)。鉴于这将是一个经常使用的路由,我想避免迭代遍历数组,以便每次都找到允许的值。有更简单的方法吗?换句话说,我知道我可以使用:

 permittedValues = [];
 for (i = 0; i < array.length; i++){
    permittedValues[i] = array[i]["key"];
 }

but I'd like to avoid this for loop, if possible.

但如果可能的话,我想避免这种循环。

P.S: This seems to be a fundamental question and the answers I found online didn't exactly answer my question. So, apologies if this has already been asked and answered.

P.S:这似乎是一个基本问题,我在网上找到的答案并没有完全回答我的问题。所以,如果已经被要求和回答,请道歉。

4 个解决方案

#1


11  

You could map:

你可以映射:

permittedValues = array.map(function(value) {
  return value.key;
});

In ES6/ES2015 it's even prettier with arrow functions:

在ES6 / ES2015中,箭头功能更漂亮:

permittedValues = array.map(value => value.key);

It might be prettier, but it's probably not faster than a for() loop.

它可能更漂亮,但它可能不比for()循环快。

#2


4  

Using lodash,

Since lodash 4.x the _.pluck function has been removed in support to map function.

由于lodash 4.x已删除_.pluck函数以支持map函数。

so you can achieve the desired task by:

所以你可以通过以下方式完成所需的任务

import _ from 'lodash'
_.map(items, 'key');

Ref: What happened to Lodash _.pluck?

参考:Lodash _.pluck发生了什么事?

#3


1  

In the current versions of Javascript you need a loop do to it.

在当前版本的Javascript中,您需要循环执行它。

However you can use a module like npm `lodash' to make it look simpler

但是你可以使用像npm`lodash'这样的模块使它看起来更简单

var _ = require('lodash')
var permittedValues = _.pluck(array, 'key')

link to pluck documentation

链接到pluck文档

#4


0  

You could extract the values via map, and form them into a regex to match values against.

您可以通过map提取值,并将它们形成一个正则表达式以匹配值。

Example: http://repl.it/X0V

var items=
[
    {
        key: 'blah',
        value: 'Blah Blah'
    },
    {
        key: 'foo',
        value: 'Foos'
    },
    {
        key: 'bar',
        value: 'Bars'
    },
    {
        key: 'baz',
        value: 'Bazingo'
    }
];

var toReg = items.map(function(obj){
    return obj.key; 
}).join('|');
var regex = new RegExp('^('+ toReg +')$');

//To test the regex
var itemsToTest = ['blah', 'Pies', 'foo', 'Bazingo'];
itemsToTest.forEach(function(key){
   if(regex.test(key)){
       console.log(key);
   }
});

#1


11  

You could map:

你可以映射:

permittedValues = array.map(function(value) {
  return value.key;
});

In ES6/ES2015 it's even prettier with arrow functions:

在ES6 / ES2015中,箭头功能更漂亮:

permittedValues = array.map(value => value.key);

It might be prettier, but it's probably not faster than a for() loop.

它可能更漂亮,但它可能不比for()循环快。

#2


4  

Using lodash,

Since lodash 4.x the _.pluck function has been removed in support to map function.

由于lodash 4.x已删除_.pluck函数以支持map函数。

so you can achieve the desired task by:

所以你可以通过以下方式完成所需的任务

import _ from 'lodash'
_.map(items, 'key');

Ref: What happened to Lodash _.pluck?

参考:Lodash _.pluck发生了什么事?

#3


1  

In the current versions of Javascript you need a loop do to it.

在当前版本的Javascript中,您需要循环执行它。

However you can use a module like npm `lodash' to make it look simpler

但是你可以使用像npm`lodash'这样的模块使它看起来更简单

var _ = require('lodash')
var permittedValues = _.pluck(array, 'key')

link to pluck documentation

链接到pluck文档

#4


0  

You could extract the values via map, and form them into a regex to match values against.

您可以通过map提取值,并将它们形成一个正则表达式以匹配值。

Example: http://repl.it/X0V

var items=
[
    {
        key: 'blah',
        value: 'Blah Blah'
    },
    {
        key: 'foo',
        value: 'Foos'
    },
    {
        key: 'bar',
        value: 'Bars'
    },
    {
        key: 'baz',
        value: 'Bazingo'
    }
];

var toReg = items.map(function(obj){
    return obj.key; 
}).join('|');
var regex = new RegExp('^('+ toReg +')$');

//To test the regex
var itemsToTest = ['blah', 'Pies', 'foo', 'Bazingo'];
itemsToTest.forEach(function(key){
   if(regex.test(key)){
       console.log(key);
   }
});