map 函数演示
2025年1月19日小于 1 分钟
map 函数演示
需求
// 封装数据
const const medicalData: {
labl_name_chn: string;
category: string;
list_name_chn: string;
}[]
medicalData = [
{ labl_name_chn: string
labl_name_chn: "医疗机构代码", category: string
category: "医疗机构", list_name_chn: string
list_name_chn: "基本信息" },
{ labl_name_chn: string
labl_name_chn: "统一社会信用代码", category: string
category: "医疗机构", list_name_chn: string
list_name_chn: "基本信息" },
{ labl_name_chn: string
labl_name_chn: "医疗机构名称", category: string
category: "医疗机构", list_name_chn: string
list_name_chn: "基本信息" },
{ labl_name_chn: string
labl_name_chn: "医疗机构简称", category: string
category: "医疗机构", list_name_chn: string
list_name_chn: "基本信息" },
{ labl_name_chn: string
labl_name_chn: "主要负责人", category: string
category: "医疗机构", list_name_chn: string
list_name_chn: "基本信息" },
// ... 其他数据项
{ labl_name_chn: string
labl_name_chn: "信用等级名称", category: string
category: "医疗机构", list_name_chn: string
list_name_chn: "基本信息" },
{ labl_name_chn: string
labl_name_chn: "上级医疗机构代码", category: string
category: "医疗机构", list_name_chn: string
list_name_chn: "基本信息" },
{ labl_name_chn: string
labl_name_chn: "机构性质", category: string
category: "医疗机构", list_name_chn: string
list_name_chn: "基本信息" },
];
const const ds: {
obj_id: string;
code: null;
list_id: string;
poolarea_no: string;
labl_id: string;
labl_name: string;
labl_name_chn: string;
col_name: string;
list_name_chn: string;
updt_frqu: null;
data_ymd: null;
datatype: string;
act_stas: string;
data_fmt: null;
}[]
ds = [
{
obj_id: string
obj_id: "1",
code: null
code: null,
list_id: string
list_id: "11111111111111111111",
poolarea_no: string
poolarea_no: "370100",
labl_id: string
labl_id: "11111111111111111110",
labl_name: string
labl_name: "MedicalIinstitutionCode",
labl_name_chn: string
labl_name_chn: "医疗机构代码",
col_name: string
col_name: "MedicalIinstitutionCode",
list_name_chn: string
list_name_chn: "基本信息",
updt_frqu: null
updt_frqu: null,
data_ymd: null
data_ymd: null,
datatype: string
datatype: "String",
act_stas: string
act_stas: "1",
data_fmt: null
data_fmt: null,
},
];
将 medicalData 的 labl_name_chn 和 list_name_chn 属性循环放进 ds 对应的属性中。用数组的 map 方法
代码
详情
const const medicalData: {
labl_name_chn: string;
category: string;
list_name_chn: string;
}[]
medicalData = [
{ labl_name_chn: string
labl_name_chn: "医疗机构代码", category: string
category: "医疗机构", list_name_chn: string
list_name_chn: "基本信息" },
{ labl_name_chn: string
labl_name_chn: "统一社会信用代码", category: string
category: "医疗机构", list_name_chn: string
list_name_chn: "基本信息" },
{ labl_name_chn: string
labl_name_chn: "医疗机构名称", category: string
category: "医疗机构", list_name_chn: string
list_name_chn: "基本信息" },
{ labl_name_chn: string
labl_name_chn: "医疗机构简称", category: string
category: "医疗机构", list_name_chn: string
list_name_chn: "基本信息" },
{ labl_name_chn: string
labl_name_chn: "主要负责人", category: string
category: "医疗机构", list_name_chn: string
list_name_chn: "基本信息" },
// ... 其他数据项
{ labl_name_chn: string
labl_name_chn: "信用等级名称", category: string
category: "医疗机构", list_name_chn: string
list_name_chn: "基本信息" },
{ labl_name_chn: string
labl_name_chn: "上级医疗机构代码", category: string
category: "医疗机构", list_name_chn: string
list_name_chn: "基本信息" },
{ labl_name_chn: string
labl_name_chn: "机构性质", category: string
category: "医疗机构", list_name_chn: string
list_name_chn: "基本信息" },
];
const const ds: {
obj_id: string;
code: null;
list_id: string;
poolarea_no: string;
labl_id: string;
labl_name: string;
labl_name_chn: string;
col_name: string;
list_name_chn: string;
updt_frqu: null;
data_ymd: null;
datatype: string;
act_stas: string;
data_fmt: null;
}[]
ds = [
{
obj_id: string
obj_id: "1",
code: null
code: null,
list_id: string
list_id: "11111111111111111111",
poolarea_no: string
poolarea_no: "370100",
labl_id: string
labl_id: "11111111111111111110",
labl_name: string
labl_name: "MedicalIinstitutionCode",
labl_name_chn: string
labl_name_chn: "医疗机构代码",
col_name: string
col_name: "MedicalIinstitutionCode",
list_name_chn: string
list_name_chn: "基本信息",
updt_frqu: null
updt_frqu: null,
data_ymd: null
data_ymd: null,
datatype: string
datatype: "String",
act_stas: string
act_stas: "1",
data_fmt: null
data_fmt: null,
},
];
const const dsItem: {
obj_id: string;
code: null;
list_id: string;
poolarea_no: string;
labl_id: string;
labl_name: string;
labl_name_chn: string;
col_name: string;
list_name_chn: string;
updt_frqu: null;
data_ymd: null;
datatype: string;
act_stas: string;
data_fmt: null;
}
dsItem = const ds: {
obj_id: string;
code: null;
list_id: string;
poolarea_no: string;
labl_id: string;
labl_name: string;
labl_name_chn: string;
col_name: string;
list_name_chn: string;
updt_frqu: null;
data_ymd: null;
datatype: string;
act_stas: string;
data_fmt: null;
}[]
ds[0];
type type DsItem = {
obj_id: string;
code: null;
list_id: string;
poolarea_no: string;
labl_id: string;
labl_name: string;
labl_name_chn: string;
col_name: string;
list_name_chn: string;
updt_frqu: null;
data_ymd: null;
datatype: string;
act_stas: string;
data_fmt: null;
}
DsItem = typeof const dsItem: {
obj_id: string;
code: null;
list_id: string;
poolarea_no: string;
labl_id: string;
labl_name: string;
labl_name_chn: string;
col_name: string;
list_name_chn: string;
updt_frqu: null;
data_ymd: null;
datatype: string;
act_stas: string;
data_fmt: null;
}
dsItem;
const const res: {
obj_id: string;
code: null;
list_id: string;
poolarea_no: string;
labl_id: string;
labl_name: string;
labl_name_chn: string;
col_name: string;
list_name_chn: string;
updt_frqu: null;
data_ymd: null;
datatype: string;
act_stas: string;
data_fmt: null;
}[]
res = const medicalData: {
labl_name_chn: string;
category: string;
list_name_chn: string;
}[]
medicalData.Array<{ labl_name_chn: string; category: string; list_name_chn: string; }>.map<{
obj_id: string;
code: null;
list_id: string;
poolarea_no: string;
labl_id: string;
labl_name: string;
labl_name_chn: string;
col_name: string;
list_name_chn: string;
updt_frqu: null;
data_ymd: null;
datatype: string;
act_stas: string;
data_fmt: null;
}>(callbackfn: (value: {
...;
}, index: number, array: {
...;
}[]) => {
obj_id: string;
code: null;
list_id: string;
poolarea_no: string;
labl_id: string;
labl_name: string;
labl_name_chn: string;
col_name: string;
list_name_chn: string;
updt_frqu: null;
data_ymd: null;
datatype: string;
act_stas: string;
data_fmt: null;
}, thisArg?: any): {
obj_id: string;
code: null;
list_id: string;
poolarea_no: string;
labl_id: string;
labl_name: string;
labl_name_chn: string;
col_name: string;
list_name_chn: string;
updt_frqu: null;
data_ymd: null;
datatype: string;
act_stas: string;
data_fmt: null;
}[]
Calls a defined callback function on each element of an array, and returns an array that contains the results.map<type DsItem = {
obj_id: string;
code: null;
list_id: string;
poolarea_no: string;
labl_id: string;
labl_name: string;
labl_name_chn: string;
col_name: string;
list_name_chn: string;
updt_frqu: null;
data_ymd: null;
datatype: string;
act_stas: string;
data_fmt: null;
}
DsItem>((medical: {
labl_name_chn: string;
category: string;
list_name_chn: string;
}
medical) => {
return var Object: ObjectConstructor
Provides functionality common to all JavaScript objects.Object.ObjectConstructor.assign<{}, {
obj_id: string;
code: null;
list_id: string;
poolarea_no: string;
labl_id: string;
labl_name: string;
labl_name_chn: string;
col_name: string;
list_name_chn: string;
updt_frqu: null;
data_ymd: null;
datatype: string;
act_stas: string;
data_fmt: null;
}, {
...;
}>(target: {}, source1: {
obj_id: string;
code: null;
list_id: string;
poolarea_no: string;
labl_id: string;
labl_name: string;
labl_name_chn: string;
col_name: string;
list_name_chn: string;
updt_frqu: null;
data_ymd: null;
datatype: string;
act_stas: string;
data_fmt: null;
}, source2: {
...;
}): {
obj_id: string;
code: null;
list_id: string;
poolarea_no: string;
labl_id: string;
labl_name: string;
labl_name_chn: string;
col_name: string;
list_name_chn: string;
updt_frqu: null;
data_ymd: null;
datatype: string;
act_stas: string;
data_fmt: null;
} & {
...;
} (+3 overloads)
Copy the values of all of the enumerable own properties from one or more source objects to a
target object. Returns the target object.assign({}, const dsItem: {
obj_id: string;
code: null;
list_id: string;
poolarea_no: string;
labl_id: string;
labl_name: string;
labl_name_chn: string;
col_name: string;
list_name_chn: string;
updt_frqu: null;
data_ymd: null;
datatype: string;
act_stas: string;
data_fmt: null;
}
dsItem, {
labl_name_chn: string
labl_name_chn: medical: {
labl_name_chn: string;
category: string;
list_name_chn: string;
}
medical.labl_name_chn: string
labl_name_chn,
list_name_chn: string
list_name_chn: medical: {
labl_name_chn: string;
category: string;
list_name_chn: string;
}
medical.list_name_chn: string
list_name_chn,
});
});
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v20.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v20.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v20.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```console.Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v20.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v20.x/api/util.html#utilformatformat-args) for more information.log(" ? res ", const res: {
obj_id: string;
code: null;
list_id: string;
poolarea_no: string;
labl_id: string;
labl_name: string;
labl_name_chn: string;
col_name: string;
list_name_chn: string;
updt_frqu: null;
data_ymd: null;
datatype: string;
act_stas: string;
data_fmt: null;
}[]
res);