আপনি যদি কোনও ES6- সক্ষম পরিবেশে প্রোগ্রামিং করছেন (যেমন নোড.জেএস, আপনার প্রয়োজনীয় ES6 ক্ষমতা সহ একটি নির্দিষ্ট ব্রাউজার বা আপনার পরিবেশের জন্য ES6 কোডটি স্থানান্তর করছেন) তবে আপনি SetES6 তে অন্তর্নির্মিত অবজেক্টটি ব্যবহার করতে পারেন । এটিতে খুব সুন্দর ক্ষমতা রয়েছে এবং এটি আপনার পরিবেশে ঠিক যেমন ব্যবহার করা যায়।
ES5 পরিবেশে অনেকগুলি সাধারণ জিনিসের জন্য, অবজেক্ট ব্যবহার করা খুব ভাল কাজ করে। যদি objআপনার অবজেক্ট হয় এবং Aকোনও ভেরিয়েবলের মান রয়েছে যা আপনি সেটটিতে পরিচালনা করতে চান তবে এটি আপনি করতে পারেন:
সূচনা কোড:
// create empty object
var obj = {};
// or create an object with some items already in it
var obj = {"1":true, "2":true, "3":true, "9":true};
প্রশ্ন 1: Is Aতালিকায়:
if (A in obj) {
// put code here
}
প্রশ্ন 2: তালিকা থেকে 'এ' মুছুন যদি তা সেখানে থাকে:
delete obj[A];
প্রশ্ন 3: তালিকাটিতে 'এ' যুক্ত করুন যদি এটি ইতিমধ্যে না থাকে
obj[A] = true;
সম্পূর্ণতার জন্য, Aতালিকায় রয়েছে কিনা এর জন্য পরীক্ষাটি এটির সাথে কিছুটা নিরাপদ:
if (Object.prototype.hasOwnProperty.call(obj, A))
// put code here
}
কারণ constructorসম্পত্তি হিসাবে বেস অবজেক্টে এবং / বা বৈশিষ্ট্যগুলির মধ্যে অন্তর্নির্মিত সম্ভাব্য দ্বন্দ্ব ।
ES6 এর সাইডবার: ECMAScript 6 বা ES 2015 নামক কিছু কিছু বর্তমান সংস্করণে একটি বিল্ট-ইন সেট অবজেক্ট রয়েছে । এটি এখন কয়েকটি ব্রাউজারে প্রয়োগ করা হয়েছে। যেহেতু ব্রাউজার প্রাপ্যতা সময়ের সাথে পরিবর্তিত হয়ে আপনি লাইন তাকান জন্য করতে Setমধ্যে এই ES6 সামঞ্জস্য টেবিল ব্রাউজার প্রাপ্যতা জন্য বর্তমান অবস্থা দেখতে।
অন্তর্নির্মিত সেট অবজেক্টের একটি সুবিধা হ'ল এটি হ'ল অবজেক্টের মতো স্ট্রিংয়ের সমস্ত কীগুলিকে বাধ্য করে না তাই আপনার পৃথক কী হিসাবে 5 এবং "5" উভয়ই থাকতে পারে। এবং আপনি এমনকি স্ট্রিং রূপান্তর ছাড়াই অবজেক্টগুলিকে সেটে সরাসরি ব্যবহার করতে পারেন। এখানে একটি নিবন্ধ যা কিছু কিছু দক্ষতা এবং সেট অবজেক্টে MDN এর ডকুমেন্টেশন বর্ণনা করে।
আমি এখন ES6 সেট অবজেক্টের জন্য একটি পলিফিল লিখেছি যাতে আপনি এখনই এটি ব্যবহার শুরু করতে পারেন এবং ব্রাউজার সমর্থন করলে এটি স্বয়ংক্রিয়ভাবে বিল্ট-ইন সেট অবজেক্টের কাছে পিছিয়ে যাবে। এটির সুবিধা রয়েছে যে আপনি ES6 সাথে সামঞ্জস্যপূর্ণ কোড লিখছেন যা আইই 7-তে ফিরে সমস্তরকম কাজ করবে। তবে, কিছুটা ডাউনসাইড রয়েছে। ES6 সেট ইন্টারফেস ES6 for (item of mySet)পুনরাবৃত্তির সুবিধা গ্রহণ করে যাতে আপনি এর মতো কাজ করতে পারেন এবং এটি স্বয়ংক্রিয়ভাবে আপনার জন্য সেটটির মাধ্যমে পুনরাবৃত্তি হবে। তবে, এই জাতীয় ভাষার বৈশিষ্ট্যটি পলিফিলের মাধ্যমে প্রয়োগ করা যায় না। আপনি এখনও নতুন ES6 ভাষা বৈশিষ্ট্যগুলি ব্যবহার না করে একটি ES6 সেটটি পুনরাবৃত্তি করতে পারেন, তবে নতুন ভাষার বৈশিষ্ট্যগুলি ছাড়াই, এটি নীচে অন্তর্ভুক্ত অন্যান্য সেট ইন্টারফেসের মতো সুবিধাজনক নয়।
উভয়টি দেখার পরে কোনটি আপনার পক্ষে সবচেয়ে ভাল কাজ করে তা আপনি সিদ্ধান্ত নিতে পারেন। ES6 সেট পলফিলটি এখানে রয়েছে: https://github.com/jfriend00/ES6- সেট ।
এফওয়াইআই, আমার নিজের পরীক্ষায়, আমি লক্ষ্য করেছি যে ফায়ারফক্স ভি 29 সেট বাস্তবায়ন অনুমানের বর্তমান খসড়াটিতে সম্পূর্ণ আপ টু ডেট নয়। উদাহরণস্বরূপ, আপনি .add()স্পেক বর্ণিত এবং আমার পলফিল সমর্থন মতো পদ্ধতি কলগুলি চেইন করতে পারবেন না । এটি সম্ভবত গতিতে নির্দিষ্টকরণের বিষয় কারণ এটি এখনও চূড়ান্ত হয়নি।
প্রাক-বিল্ট সেট অবজেক্টস: আপনি যদি এমন কোনও পূর্বনির্মাণ বস্তু চান যা আপনি যে কোনও ব্রাউজারে ব্যবহার করতে পারেন এমন কোনও সেটে অপারেটিংয়ের জন্য পদ্ধতি রয়েছে তবে আপনি বিভিন্ন প্রাক-বিল্ট অবজেক্টের একটি সিরিজ ব্যবহার করতে পারেন যা বিভিন্ন ধরণের সেট প্রয়োগ করে। একটি মিনিসেট রয়েছে যা একটি ছোট কোড যা একটি সেট অবজেক্টের বেসিকগুলি প্রয়োগ করে। এটিতে আরও একটি বৈশিষ্ট্য সমৃদ্ধ সেট অবজেক্ট এবং একটি অভিধান সহ একাধিক উপকরণ রয়েছে (আসুন আপনি প্রতিটি কীটির জন্য একটি মান সংরক্ষণ / পুনরুদ্ধার করতে পারেন) এবং একটি অবজেক্টসেট (যাক আপনি বস্তুর একটি সেট রাখুন - হয় জেএস অবজেক্ট বা ডিওএম অবজেক্ট যেখানে আপনি সরবরাহ করেন সেখানে ফাংশন যা প্রত্যেকের জন্য একটি অনন্য কী উত্পন্ন করে বা অবজেক্টসটি আপনার জন্য কী উত্পন্ন করবে)।
এখানে miniSet কোড একটি কপি (অধিকাংশ আপ-টু-ডেট কোড হয় GitHub এখানে )।
"use strict";
//-------------------------------------------
// Simple implementation of a Set in javascript
//
// Supports any element type that can uniquely be identified
// with its string conversion (e.g. toString() operator).
// This includes strings, numbers, dates, etc...
// It does not include objects or arrays though
// one could implement a toString() operator
// on an object that would uniquely identify
// the object.
//
// Uses a javascript object to hold the Set
//
// This is a subset of the Set object designed to be smaller and faster, but
// not as extensible. This implementation should not be mixed with the Set object
// as in don't pass a miniSet to a Set constructor or vice versa. Both can exist and be
// used separately in the same project, though if you want the features of the other
// sets, then you should probably just include them and not include miniSet as it's
// really designed for someone who just wants the smallest amount of code to get
// a Set interface.
//
// s.add(key) // adds a key to the Set (if it doesn't already exist)
// s.add(key1, key2, key3) // adds multiple keys
// s.add([key1, key2, key3]) // adds multiple keys
// s.add(otherSet) // adds another Set to this Set
// s.add(arrayLikeObject) // adds anything that a subclass returns true on _isPseudoArray()
// s.remove(key) // removes a key from the Set
// s.remove(["a", "b"]); // removes all keys in the passed in array
// s.remove("a", "b", ["first", "second"]); // removes all keys specified
// s.has(key) // returns true/false if key exists in the Set
// s.isEmpty() // returns true/false for whether Set is empty
// s.keys() // returns an array of keys in the Set
// s.clear() // clears all data from the Set
// s.each(fn) // iterate over all items in the Set (return this for method chaining)
//
// All methods return the object for use in chaining except when the point
// of the method is to return a specific value (such as .keys() or .isEmpty())
//-------------------------------------------
// polyfill for Array.isArray
if(!Array.isArray) {
Array.isArray = function (vArg) {
return Object.prototype.toString.call(vArg) === "[object Array]";
};
}
function MiniSet(initialData) {
// Usage:
// new MiniSet()
// new MiniSet(1,2,3,4,5)
// new MiniSet(["1", "2", "3", "4", "5"])
// new MiniSet(otherSet)
// new MiniSet(otherSet1, otherSet2, ...)
this.data = {};
this.add.apply(this, arguments);
}
MiniSet.prototype = {
// usage:
// add(key)
// add([key1, key2, key3])
// add(otherSet)
// add(key1, [key2, key3, key4], otherSet)
// add supports the EXACT same arguments as the constructor
add: function() {
var key;
for (var i = 0; i < arguments.length; i++) {
key = arguments[i];
if (Array.isArray(key)) {
for (var j = 0; j < key.length; j++) {
this.data[key[j]] = key[j];
}
} else if (key instanceof MiniSet) {
var self = this;
key.each(function(val, key) {
self.data[key] = val;
});
} else {
// just a key, so add it
this.data[key] = key;
}
}
return this;
},
// private: to remove a single item
// does not have all the argument flexibility that remove does
_removeItem: function(key) {
delete this.data[key];
},
// usage:
// remove(key)
// remove(key1, key2, key3)
// remove([key1, key2, key3])
remove: function(key) {
// can be one or more args
// each arg can be a string key or an array of string keys
var item;
for (var j = 0; j < arguments.length; j++) {
item = arguments[j];
if (Array.isArray(item)) {
// must be an array of keys
for (var i = 0; i < item.length; i++) {
this._removeItem(item[i]);
}
} else {
this._removeItem(item);
}
}
return this;
},
// returns true/false on whether the key exists
has: function(key) {
return Object.prototype.hasOwnProperty.call(this.data, key);
},
// tells you if the Set is empty or not
isEmpty: function() {
for (var key in this.data) {
if (this.has(key)) {
return false;
}
}
return true;
},
// returns an array of all keys in the Set
// returns the original key (not the string converted form)
keys: function() {
var results = [];
this.each(function(data) {
results.push(data);
});
return results;
},
// clears the Set
clear: function() {
this.data = {};
return this;
},
// iterate over all elements in the Set until callback returns false
// myCallback(key) is the callback form
// If the callback returns false, then the iteration is stopped
// returns the Set to allow method chaining
each: function(fn) {
this.eachReturn(fn);
return this;
},
// iterate all elements until callback returns false
// myCallback(key) is the callback form
// returns false if iteration was stopped
// returns true if iteration completed
eachReturn: function(fn) {
for (var key in this.data) {
if (this.has(key)) {
if (fn.call(this, this.data[key], key) === false) {
return false;
}
}
}
return true;
}
};
MiniSet.prototype.constructor = MiniSet;