// Note that `array.sort()` modifies the original array, it's an in-place operation.
array.sort();
returnarray;
}
sortArray([
'Tomato',
'Strawberry',
'Potato',
'Eggplant',
'Carrot',
]);
// We define a function that returns a set.
functioncreateBasicSet(){
// We have to explicitly create a new Set object.
// Each element in a set can be any expression.
// Note that duplicated elements appear only once in the Set.
returnnewSet([1,1,1,2,1+2]);
}
createBasicSet();
functionsizeOfSet(set){
returnset.size;
}
// >> Add 'D' to the array like `Set(['A', 'B', 'C', 'D'])` and see what happens.
sizeOfSet(newSet(['A','B','C']));
functionsetContainsElement(set,value){
returnset.has(value);
}
// >> Change the value from 'B' to 'X' see if the set contains 'X' and see what happens.
setContainsElement(newSet(['A','B','C']),'B');
functionaddElementToSet(set,value){
set.add(value);
returnset;
}
addElementToSet(newSet(['A','B','C']),'D');
functiondeleteElementFromSet(set,value){
set.delete(value);
returnset;
}
deleteElementFromSet(newSet(['A','B','C']),'C');
functionsetToArray(set){
returnArray.from(set.values());
}
setToArray(newSet(['A','B','C']));
functioncreateBasicMap(){
// Note that only the *latest* duplicated keys are kept.
returnnewMap([
['🍓','Strawberry'],
['🥕','Carrot'],
['🍆','Eggplant'],
['🍅','Tomato'],
['🥔','Previous duplicated keys are ignored'],
['🥔','Potato'],
]);
}
createBasicMap();
functionsizeOfMap(map){
returnmap.size;
}
sizeOfMap(newMap([
['🍓','Strawberry'],
['🥕','Carrot'],
['🍆','Eggplant'],
['🍅','Tomato'],
['🥔','Potato'],
]));
functionmapContainsKey(map,key){
returnmap.has(key);
}
// >> Change the key from '🥕' to 'X' and see what happens.
mapContainsKey(
newMap([
['🍓','Strawberry'],
['🥕','Carrot'],
['🍆','Eggplant'],
['🍅','Tomato'],
['🥔','Potato'],
]),
'🥕',// the key we're looking for
);
functiongetKeysValueFromMap(map,key){
returnmap.get(key);
}
// >> Change the key from '🥕' to 'X' and see what happens.
getKeysValueFromMap(
newMap([
['🍓','Strawberry'],
['🥕','Carrot'],
['🍆','Eggplant'],
['🍅','Tomato'],
['🥔','Potato'],
]),
'🥕',// the key we're looking for
);
functionsetKeyValuePairInMap(map,key,value){
map.set(key,value);
returnmap;
}
// >> Change the key from '🥕' to 'X' and see what happens.
setKeyValuePairInMap(
newMap([
['🍓','Strawberry'],
['🥕','Carrot'],
['🍆','Eggplant'],
['🍅','Tomato'],
['🥔','Potato'],
]),
'🥕',// key
'CARROT',// value
);
functiondeleteElementFromMap(set,value){
set.delete(value);
returnset;
}
deleteElementFromMap(
newMap([
['🍓','Strawberry'],
['🥕','Carrot'],
['🍆','Eggplant'],
['🍅','Tomato'],
['🥔','Potato'],
]),
'🥕',// key from the key-value pair entry we want to delete
);
// To get an Array from the Map's keys.
functionmapKeysToArray(map){
returnArray.from(map.keys());
}
mapKeysToArray(newMap([
['🍓','Strawberry'],
['🥕','Carrot'],
['🍆','Eggplant'],
['🍅','Tomato'],
['🥔','Potato'],
]));
// To get an Array from the Map's values.
functionmapValuesToArray(map){
returnArray.from(map.values());
}
mapValuesToArray(newMap([
['🍓','Strawberry'],
['🥕','Carrot'],
['🍆','Eggplant'],
['🍅','Tomato'],
['🥔','Potato'],
]));
Purpose-built for displays of data
Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.