Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

set js

// Use to remove duplicate elements from the array 

const numbers = [2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5]

//spreading numbers of the object into an array using the new operator
console.log([...new Set(numbers)]) 

// [2, 3, 4, 5, 6, 7, 32]
Comment

set in javascript

let mySet = new Set()

mySet.add(1)           // Set [ 1 ]
mySet.add(5)           // Set [ 1, 5 ]
mySet.add(5)           // Set [ 1, 5 ]
mySet.add('some text') // Set [ 1, 5, 'some text' ]
let o = {a: 1, b: 2}
mySet.add(o)

mySet.add({a: 1, b: 2})   // o is referencing a different object, so this is okay

mySet.has(1)              // true
mySet.has(3)              // false, since 3 has not been added to the set
mySet.has(5)              // true
mySet.has(Math.sqrt(25))  // true
mySet.has('Some Text'.toLowerCase()) // true
mySet.has(o)       // true

mySet.size         // 5

mySet.delete(5)    // removes 5 from the set
mySet.has(5)       // false, 5 has been removed

mySet.size         // 4, since we just removed one value

console.log(mySet)
// logs Set(4) [ 1, "some text", {…}, {…} ] in Firefox
// logs Set(4) { 1, "some text", {…}, {…} } in Chrome
Comment

set js

let text = 'India'

let mySet = new Set(text)  // Set ['I', 'n', 'd', 'i', 'a']
mySet.size  // 5

//case sensitive & duplicate ommision
new Set("Firefox")  // Set(7) [ "F", "i", "r", "e", "f", "o", "x" ]
new Set("firefox")  // Set(6) [ "f", "i", "r", "e", "o", "x" ]
Comment

set javascript

// Use to remove duplicate elements from the array 

const numbers = [2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5]

//spreading numbers of the object into an array using the new operator
console.log([...new Set(numbers)]) 

// [2, 3, 4, 5, 6, 7, 32]
Comment

what is new set in javascript

A set is a collection of items which are unique i.e no element can be repeated. 
Set in ES6 are ordered: elements of the set can be iterated in the insertion 
order. Set can store any types of values whether primitive or objects. 
var set4 = new Set();
Comment

js set

// Use to remove duplicate elements from the array 

const array = [2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5]

//spreading numbers of the object into an array using the new operator
console.log([...new Set(array)]) 

// [2, 3, 4, 5, 6, 7, 32]
Comment

javaScript new Set() Method

// Create a Set
const letters = new Set(["a","b","c"]);
Comment

JavaScript Sets

// Create a Set
const letters = new Set();

// Add Values to the Set
letters.add("a");
letters.add("b");
letters.add("c");
Comment

set js

// iterate over items in set
// logs the items in the order: 1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}
for (let item of mySet) console.log(item)

// logs the items in the order: 1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}
for (let item of mySet.keys()) console.log(item)

// logs the items in the order: 1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}
for (let item of mySet.values()) console.log(item)

// logs the items in the order: 1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}
// (key and value are the same here)
for (let [key, value] of mySet.entries()) console.log(key)

// convert Set object to an Array object, with Array.from
let myArr = Array.from(mySet) // [1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}]

// the following will also work if run in an HTML document
mySet.add(document.body)
mySet.has(document.querySelector('body')) // true

// converting between Set and Array
mySet2 = new Set([1, 2, 3, 4])
mySet2.size                    // 4
[...mySet2]                    // [1, 2, 3, 4]

// intersect can be simulated via
let intersection = new Set([...set1].filter(x => set2.has(x)))

// difference can be simulated via
let difference = new Set([...set1].filter(x => !set2.has(x)))

// Iterate set entries with forEach()
mySet.forEach(function(value) {
  console.log(value)
})

// 1
// 2
// 3
// 4
Comment

set in javascript

const mySet1 = new Set()

mySet1.add(1)           // Set [ 1 ]
mySet1.add(5)           // Set [ 1, 5 ]
mySet1.add(5)           // Set [ 1, 5 ]
mySet1.add('some text') // Set [ 1, 5, 'some text' ]
const o = {a: 1, b: 2}
mySet1.add(o)

mySet1.add({a: 1, b: 2})   // o is referencing a different object, so this is okay

mySet1.has(1)              // true
mySet1.has(3)              // false, since 3 has not been added to the set
mySet1.has(5)              // true
mySet1.has(Math.sqrt(25))  // true
mySet1.has('Some Text'.toLowerCase()) // true
mySet1.has(o)       // true

mySet1.size         // 5

mySet1.delete(5)    // removes 5 from the set
mySet1.has(5)       // false, 5 has been removed

mySet1.size         // 4, since we just removed one value

mySet1.add(5)       // Set [1, 'some text', {...}, {...}, 5] - a previously deleted item will be added as a new item, it will not retain its original position before deletion

console.log(mySet1)
// logs Set(5) [ 1, "some text", {…}, {…}, 5 ] in Firefox
// logs Set(5) { 1, "some text", {…}, {…}, 5 } in Chrome
Comment

set js

const myArray = ['value1', 'value2', 'value3'];

// Use the regular Set constructor to transform an Array into a Set
const mySet = new Set(myArray);

mySet.has('value1')     // returns true

// Use the spread syntax to transform a set into an Array.
console.log([...mySet]); // Will show you exactly the same Array as myArray
Comment

Create JavaScript Set

// create Set
const set1 = new Set(); // an empty set
console.log(set1); // Set {}

// Set with multiple types of value
const set2 = new Set([1, 'hello', {count : true}]);
console.log(set2); // Set {1, "hello", {count: true}}
Comment

javaScript set() Method

// Create a Map
const fruits = new Map();

// Set Map Values
fruits.set("apples", 500);
fruits.set("bananas", 300);
fruits.set("oranges", 200);
Comment

js Set

const id = new Set();

id.add(1);
id.add(2);
id.add(3);
id.add(3);// duplicate value will not be added
console.log(id);// output: Set {1, 2, 3}
id.delete(2);// delete the value 2 from the set
console.log(id);// output: Set {1, 3}
id.has(2);// check if the set has the value 2 // output: true
console.log(id);


//order is not guaranteed
Comment

set method in javascript

const language = {
  set current(name) {
    this.log.push(name);
  },
  log: []
};

language.current = 'EN';
language.current = 'FA';

console.log(language.log);
// expected output: Array ["EN", "FA"]
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to use cookies in react class component 
Javascript :: substr javascript 
Javascript :: export table data to excel using javascript or jquery 
Javascript :: setstate in react 
Javascript :: negate regular expression 
Javascript :: jquery get all data attributes values 
Javascript :: lexical scope javascript 
Javascript :: jquery replace multiple words 
Javascript :: documentation tool for angular applications 
Javascript :: Parse BSON to JSON 
Javascript :: use jquery in project using NPM 
Javascript :: javascript swap array elements 
Javascript :: navigate json object javascript 
Javascript :: Aug 25 2020 00:00:00 GMT+0530 
Javascript :: navigator user media check if camera is availabe 
Javascript :: Destructuring of object in ES6 
Javascript :: how to reverse array in javascript 
Javascript :: javascript add query string to url 
Javascript :: sort array in javascript 
Javascript :: open new window in java script 
Javascript :: date javascript 
Javascript :: javascript pop object from array 
Javascript :: javascript strftime 
Javascript :: from json timestamp to date python 
Javascript :: svelte wait 
Javascript :: push json data into a list of objects in flutter 
Javascript :: how to get data from for loop in react native 
Javascript :: Query all object in mongo array to satisy condition 
Javascript :: create 2d array in javascript filled with 0 
Javascript :: how to add image url in tailwindconfig .js 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =