admin 管理员组文章数量: 1086019
I need a bit of help to remove items from an array. I have a number of check boxes, each have a dynamically created data attribute. On un-checking a check box I want to remove items from an array which match item.sector value of each array item. I can't quite get it working. Any help would be appreciated.
let mapMarkers = [];
function addFilter(self) {
const markerObject = filterObject[self.id];
const markerID = markerObject[0]["id"];
const dataSetSector = self.dataset.sector;
const dataSetYear = self.dataset.year;
if (self.checked) {
mapMarkers.push(markerObject);
} else {
// data attribute SECTOR exists
if (self.hasAttribute("data-sector")) {
mapMarkers = mapMarkers.reduce((acc, curr) => {
if (curr.sector !== dataSetSector) acc.push(curr);
return acc;
});
}
// data attribute YEAR exists
else if (self.hasAttribute("data-year")) {
mapMarkers = mapMarkers.reduce((acc, curr) => {
if (curr.sector !== dataSetYear) acc.push(curr);
return acc;
});
}
}
}
I need a bit of help to remove items from an array. I have a number of check boxes, each have a dynamically created data attribute. On un-checking a check box I want to remove items from an array which match item.sector value of each array item. I can't quite get it working. Any help would be appreciated.
let mapMarkers = [];
function addFilter(self) {
const markerObject = filterObject[self.id];
const markerID = markerObject[0]["id"];
const dataSetSector = self.dataset.sector;
const dataSetYear = self.dataset.year;
if (self.checked) {
mapMarkers.push(markerObject);
} else {
// data attribute SECTOR exists
if (self.hasAttribute("data-sector")) {
mapMarkers = mapMarkers.reduce((acc, curr) => {
if (curr.sector !== dataSetSector) acc.push(curr);
return acc;
});
}
// data attribute YEAR exists
else if (self.hasAttribute("data-year")) {
mapMarkers = mapMarkers.reduce((acc, curr) => {
if (curr.sector !== dataSetYear) acc.push(curr);
return acc;
});
}
}
}
Share
Improve this question
asked Mar 13, 2018 at 13:20
SharperSharper
3571 gold badge3 silver badges18 bronze badges
1
-
1
You can use
filter
insetad ofreduce
– Artem Kolodko Commented Mar 13, 2018 at 13:21
1 Answer
Reset to default 6The second argument of reduce
is the initial value, i.e. the initial value of acc
. Since you're calling a push
method on acc
, it should probably be an array.
When you omit the second argument, the first element in your array is used as the initial value. I.e.: the first call takes (mapMarkers[0], mapMarkers[1])
.
To fix this issue, pass a new array to the reduce
method:
mapMarkers = mapMarkers.reduce((acc, curr) => {
if (curr.sector !== dataSetSector) acc.push(curr);
return acc;
}, []);
// ^--- change
Alternatively, as suggested in the ments, you can use filter
.
本文标签: javascriptusing reduce to remove item from arrayStack Overflow
版权声明:本文标题:javascript - using reduce to remove item from array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744038115a2522747.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论