「JavaScript/Set」の版間の差分
削除された内容 追加された内容
Fix Cite タグ: 2017年版ソースエディター |
→Polyfill: cleanup. タグ: 2017年版ソースエディター |
||
74 行
;[https://paiza.io/projects/Zx6wmzOUm7IMcltSD14Cug?language=javascript Polyfill]:<syntaxhighlight lang="javascript">
Object.entries({
filter: function (callback
if (
throw new TypeError();
const result = new Set()
for (const x of this)
if (callback
result.add(x)
return result
},
map: function (callback if (
throw new TypeError();
const result = new Set()
for (const x of this)
result.add(callback
return result
},
reduce: function (callback, initial) { if (
throw new TypeError();
let result = initial
for (const x of this) {
if (result ===
result = x
else
result = callback(result, x, this)
}
return result
},
union: function (other) { const result = new Set(this)
for (const x of other) {
result.add(x)
}
return result
},
intersection: function (other) { return this.filter(e => (other.has(e)))
},
difference: function (other) { return this.filter(e => (!other.has(e)))
},
superset_p: function (other) { for (
if (!this.has(elem)) {
return false
}
}
return true
},
subset_p: function (other) { return other.superset_p(this)
},
}).forEach(pair => {
Object.defineProperty(Set.prototype, pair[0], {
131 ⟶ 135行目:
})
let a = new Set([0, 1, 2]),
b = new Set([2, 3, 4]),
c = new Set([0, 1, 2, 3, 4, 5, 6])
console.log("a = ", a, ", b = ", b)
console.log(a.filter(x => x % 2))
//console.log(a.filter(
console.log(a.map(x => x * 2))
console.log(a.union(b))
console.log(a.intersection(b))
146 ⟶ 151行目:
a = Set(3) { 0, 1, 2 } , b = Set(3) { 2, 3, 4 }
Set(1) { 1 }
Set(
Set(5) { 0, 1, 2, 3, 4 }
Set(1) { 2 }
|