The goal is to map data-
attributes from one element to another, but while ignoring certain attributes such as class
, id
, etc.
Here is a block:
let ignoreList = ['class', 'id', 'name', 'value', 'type', 'src'];
$(".some-class").on("click",function(event) {
let attrMap = new Map();
let attrs = event.target.attributes;
$.each(attrs, function(e){
console.log(`"${this.name}" is in ignoreList: ` + (ignoreList.indexOf(this.name) == 0).toString());
ignoreList.indexOf(this.name) == 0 ? attrMap.set(this.name, this.value) : null;
});
console.log(attrs);
console.log(attrMap);
});
What I would have expected in the console
would be:
"class" is in ignoreList: true
"data-one" is in ignoreList: false
"data-two" is in ignoreList: false
"data-three" is in ignoreList: false
"data-four" is in ignoreList: false
"data-five" is in ignoreList: false
"data-six" is in ignoreList: false
NamedNodeMap {0: class, 1: data-one, 2: data-two, 3: data-three, 4: data-four, 5: data-five, 6: data-six, class: class, data-one: data-one, data-two: data-two, data-three: data-three, data-four: data-four, …}
Map(6) {'data-one' => 'Lorem ipsum', 'data-two' => 'dolor sit amet', 'data-three' => 'purto ludus', 'data-four' => 'indoctum sit', …}
What I am getting in the console
:
(repeated values ignored for brevity)
Map(1) {'class' => 'some-class'}
I have tried various logics in the conditional, such as < 0
, == -1
, != 0