1. ABAP内表
内表是ABAP编程过程中最常用的一种数据类型,下面是一个简单的内表程序,也即使用内表存储多个"人名数据"。
先定义结构ts_person,并基于结构定义内表变量lt_people,然后通过insert语句为内表赋值。
javascript
REPORT ztest_interal_table.
TYPES: BEGIN OF ts_person,
first_name TYPE string,
last_name TYPE string,
END OF ts_person.
DATA: ls_person TYPE ts_person,
lt_people TYPE STANDARD TABLE OF ts_person.
ls_person-first_name = 'Mick'.
ls_person-last_name = 'Wang'.
INSERT ls_person INTO TABLE lt_people.
ls_person-first_name = 'Tom'.
ls_person-last_name = 'li'.
INSERT ls_person INTO TABLE lt_people.
ls_person-first_name = 'Jone'.
ls_person-last_name = 'Xu'.
INSERT ls_person INTO TABLE lt_people.
LOOP AT lt_people ASSIGNING FIELD-SYMBOL(<ls_person>).
WRITE: / <ls_person>-first_name, <ls_person>-last_name.
ENDLOOP.
2. JavaScript的等价实现
如何用JavaScript进行等价实现呢?
首先让我们声明一个构造函数Person,用于定义类型;然后定义一个数组people来存储数据;数组中存储的是基于Person生成的对象实例。
javascript
// construct Person as object with the fields firstName and lastName
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
// create an arrary to store different people
let people = [];
people.push(new Person("Mick", "Wang"));
people.push(new Person("Tom", "Li"));
people.push(new Person("Jone", "Xu"));
// loop over the arrary and print out
for (let person of people) {
console.log(person.firstName + " " + person.lastName);
}
如果要进行类比的话,可以有如下的对应关系:
- JavaScript的function Person()实现了ABAP中定义的数据结构ts_person;
- JavaScript的数组people实现了ABAP中内表变量lt_people的功能;
- JavaScript中数组的push()方法为数组赋值,实现了ABAP中Insert关键字向内表插入数据的效果。
3. 小结
本文通过一个小的示例程序,展示了用JavaScript的函数和数组实现ABAP中内表的效果。希望本文对你有帮助!