简体   繁体   English

JavaScript Map function 在对象数组中使用通用键

[英]JavaScript Map function to use generic keys within array of objects

I have the following array of objects:我有以下对象数组:

[
    {
        "job_id": 1,
        "job_name": "Engineer"
    },
    {
        "job_id": 2,
        "job_name": "Scientist"
    },
    {
        "job_id": 3,
        "job_name": "Teacher"
    }
]

where the following code is used to construct a Material-UI Select - options here is the above array of objects:其中以下代码用于构造 Material-UI Select - 这里的选项是上面的对象数组:

{options.map(option => {
          return (
            <MenuItem key={option.job_id} value={option.job_id}>
              {option.job_name}
            </MenuItem>
          )
})}

My questions is, in order to not tie it down to actual key names of option.job_id and option.job_name as I am looking at using different datasets with different key names, but always following this key value pair format - can the map function be changed to make it more generic as to not worry about key names but still return the data for the Select dropdown, ie:我的问题是,为了不将其绑定到option.job_idoption.job_name的实际键名,因为我正在查看使用具有不同键名的不同数据集,但始终遵循此键值对格式 - map function 可以更改为使其更通用,无需担心键名,但仍返回 Select 下拉列表的数据,即:

{options.map(option => {
              return (
                <MenuItem key={option.generic_id} value={option.generic_id}>
                  {option.generic_name}
                </MenuItem>
              )
    })}

I guess I am asking whether it's possible to access the object keys via the map function without needing to know job_id and job_name ?我想我在问是否可以通过 map function 访问 object 密钥而不需要知道job_idjob_name

If you can't make the keys more generic upstream, you can do one of two things如果你不能让上游的键更通用,你可以做两件事之一

  1. Normalising the keys before they hit your component on the call site在键击中调用站点上的组件之前对其进行规范化
import { pipe, mapKeys } from 'utils';

const normaliseOption = mapKeys(key =>
      key.match(/_id$/) ? 'id'
    : key.match(/_name$/) ? 'name'
    : key
);

options.map(pipe(normaliseOption, option =>
    <MenuItem key={option.id} value={option.id}>
        {option.name}
    </MenuItem>
));
// utils
export const pipe = (...fns) => fns.reduce(
    (f, g) => x => g(f(x))
);

export const mapKeys = f => pipe(
    Object.entries,
    x => x.map(([k, v]) => [f(k), v]),
    Object.fromEntries
);

1.bis) Somewhere upstream, where you know for sure you are dealing with jobs, you can do the above without the regexp. 1.bis) 在上游的某个地方,您确定自己正在处理工作,您可以在没有正则表达式的情况下执行上述操作。

  1. Relying on a common interface to dispatch to the correct info依靠一个通用接口来调度正确的信息
options.map(option =>
    <MenuItem key={option.getId()} value={option.getId()}>
        {option.getName()}
    </MenuItem>
);
// somewhere you know you are dealing with jobs
import JobOption from './JobOption';
const options = jobs.map(JobOption.of);
export default class JobOption {
    constructor({job_id, job_name}) {
        this.id = job_id;
        this.name = job_name;
    }
    static of (descriptor) { return new JobOption(descriptor); }
    getId () { return this.id; }
    getName () { return this.name; }
}

If you want to be able to use dynamic object keys, it sounds like you want some kind of helper function that can take in the array and the relevant keys and return your menu items如果您希望能够使用动态 object 键,听起来您需要某种帮助器 function 可以接收数组和相关键并返回您的菜单项

For example例如

const menuBuilder = (options, keyProp, labelProp) => options.map(option => (
  <MenuItem key={option[keyProp]} value={option[keyProp]}>
    {option[labelProp]}
  </MenuItem>
))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript:使用未知键映射对象数组 - Javascript: map Array of Objects with unknown keys 有效地重命名/重新映射对象数组中的 javascript/json 对象键 - Efficiently rename/re-map javascript/json object keys within array of objects 访问对象中的键以在函数中使用 - Accessing keys within Objects for use in a function 对象作为 JS Map 数据类型中的键的实际用途是什么? - What is a practical use of objects as keys within JS Map dataype? 如何使用 map 函数从对象数组中返回多个键? ES6 - How to use map function to return multiple keys from an array of objects? ES6 使用重新映射的键将 JSON 数组中的对象添加到 JavaScript 数组中 - Add objects from within JSON array into JavaScript array with keys remapped Javascript 重新格式化或 map 将对象数组转换为新对象数组? 就像重新格式化它并为值提供新的键(名称) - Javascript reformat or map an array of objects to a new array of new objects? like reformating it and giving new keys (names) for values 使用对象键将javascript对象数组划分为多个数组 - Use object keys to divide javascript array of objects into multiple arrays 是否有JavaScript方法/函数通过键将对象循环到数组中? - Is there a JavaScript Method/ Function to loop Objects via Keys into a array? Javascript 对象数组的一般搜索 - Javascript generic search of array of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM