Я испытываю странное поведение с .map()
.Он возвращает пустой элемент, а .forEach()
- нет.
Вот код:
class Entry {
constructor(data) {
this.name = data[0],
this.age = data[1],
this.school = data[2]
};
get organised() {
return this.organise();
};
organise() {
const data = {
name: this.name,
school: this.school
};
return data;
}
}
const getDataForEach = (obj) => {
let r = [];
obj.forEach((i) => {
const e = new Entry(i);
r.push(e.organised);
});
return r;
};
getDataForEach(input); // return normal object array [{...}, {...}, ...]
Но если я использую .map()
, он возвращает массив объектов с первым элементом:пустой.Другие предметы такие же, как в результате .forEach()
.
const getDataMap = (obj) => {
return obj.map((i) => {
const e = new Entry(i);
console.log(e) // return normal [{...}]
console.log(e.organised) // return normal {...}
return e.organised;
});
};
getDataMap(input); // return an object array with the first item empty [<1 empty item>, {...}, {...}, ...]
Испытывали ли вы что-нибудь подобное?