Declaring variables using an array [duplicate] - javascript

This question already has answers here:
Multiple assignment in JavaScript? What does `[ a, b, c ] = [ 1, 2, 3 ]` mean?
(4 answers)
Closed 4 years ago.
I came across this code in JS Novice to Ninja.
const form = document.getElementsByTagname('form')[0];
const [input,button] = form.elements;
this create a const called input mapped to the value of the form[0]
and another const called button mapped to form[1]
is this:
const [input,button] = form.elements;
the same as:
const input = form.elements[0];
const button = form.elements[1];
Is that just some shorthand I've never come across? If so can someone tell me what it's called? Or am I misunderstanding what is happening here.

const [input,button] = form.elements;
would actually be the same as
const input = form.elements[0];
const button = form.elements[1];
This is called destructuring and it can also be used for objects.
const {value} = someObject;
would be the same as const value = someObject.value;

Related

How to get a random key value from a JavaScript object [duplicate]

This question already has answers here:
Pick random property from a Javascript object
(9 answers)
Get array of object's keys
(8 answers)
Closed 2 years ago.
I have a Javascript hash of Spanish words/phrases and their English meanings:
let phrases = {
hola: "hello",
adios: "bye",
};
I want to select a random key. I have tried for a while, and my latest attempt hasn't worked and returns undefined:
var keys = phrases.keys;
var len = phrases.length;
var rnd = Math.floor(Math.random()*len);
var key = phrases[rnd];
I've looked at other Stack Overflow answers but can't seem to find exactly what I'm looking for. Any ideas please?
Probably you can use Object.keys() instead.
Try the following:
const phrases = {
hola: "hello",
adios: "bye",
};
const keys = Object.keys(phrases);
const len = keys.length;
const rnd = Math.floor(Math.random() * len);
const key = phrases[keys[rnd]];
console.log(key);
I hope this helps!

What do the three dots "..." do in JavaScript [duplicate]

This question already has answers here:
Javascript Property with three dots (...)
(5 answers)
What does this symbol mean in JavaScript?
(1 answer)
Closed 2 years ago.
I have gone through the following JavaScript code:
let num = [1,2,2,2,3,4,5];
console.log([... new Set(nums)]); //which prints [1,2,3,4,5]
I did not understand what this ... device (three dots) does in this example.
I think you need to read Iteration first;
let nums = [1,2,2,2,3,4,5];
let setFromArray = new Set(nums);
let arrayFromSet_spread = [...setFromArray];
console.log("spread", arrayFromSet_spread);
// here the spread will iterate over iteratable and return current value
//arrayFromSet can be written without spread like this
let arrayFromSet_forOf = [];
for ( let el of setFromArray ) {
arrayFromSet_forOf.push(el)
}
console.log("forOf",arrayFromSet_forOf);
// and the most naive way to iterate :)
let arrayFromSet_ManualIteration = [];
let setFromArrayIterator = setFromArray[Symbol.iterator]();
const firstElement = setFromArrayIterator.next();
const secondElement = setFromArrayIterator.next();
const thirdElement = setFromArrayIterator.next();
const fourthElement = setFromArrayIterator.next();
const fifthElement = setFromArrayIterator.next();
const sixthElement = setFromArrayIterator.next();
arrayFromSet_ManualIteration.push(firstElement);
arrayFromSet_ManualIteration.push(secondElement);
arrayFromSet_ManualIteration.push(thirdElement);
arrayFromSet_ManualIteration.push(fourthElement);
arrayFromSet_ManualIteration.push(fifthElement);
arrayFromSet_ManualIteration.push(sixthElement);
//you could just push values not the itaration object itself
//arrayFromSet_ManualIteration.push(firstElement.value);
//arrayFromSet_ManualIteration.push(secondElement.value);
//arrayFromSet_ManualIteration.push(thirdElement.value);
//arrayFromSet_ManualIteration.push(fourthElement.value);
//arrayFromSet_ManualIteration.push(fifthElement.value);
console.log('ManualIteration full objects',arrayFromSet_ManualIteration);

How can I get a key from an object by its value? [duplicate]

This question already has answers here:
How to get a key in a JavaScript object by its value?
(31 answers)
Closed 3 years ago.
I am working in react. I have a constant like
const priorityMap = {
"medium":"clean",
"high":"breaker",
"low":"promote"
}
If I do the following , I am getting result.
const map1 = priorityMap["medium"];
console.log("print checkgroup " + map1)
But I want to do the reverse thing. My input is "clean" and I want to retrieve "medium". Is there any way to fetch the key?
It would be :
const valueToSearch = "clean"
const result = Object.entries(priorityMap).find(entry => entry[1] === valueToSearch)[1]

put array values in multiple variable Javascript [duplicate]

This question already has answers here:
Javascript equivalent of PHP's list()
(9 answers)
Closed 3 years ago.
I know I can put item by item to variable like:
var array = [15,20];
var a = array[0];
var b = array[1];
But I curious how to put array values in multi variable in one line like list() in php:
list($a,$b) = [15,20]; // $a = 15, $b = 20
Is it possible or not ?!
Use javascript's array de-structuring syntax,
var array = [15,20];
const [x, y] = array;
You can use: Destructuring assignment - Javascript 1.7
matches = ['12', 'watt'];
[value, unit] = matches;
console.log(value,unit)

immutably update value of a property in array of object [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 3 years ago.
I have this array of object
const d = [{
a_1: 1,
b_2: 2
}]
How can I update a_1 value to 2 without creating a temporary variable?
const myKey = 'a_1'
const myValue = 2
d.map(obj => ({...obj, obj[myKey]:myValue})) //why this won't work?
Remove the obj from obj[myKey] so that [myKey] is correctly seen as a computed property name.
const transformedDs = d.map(obj => ({...obj, [myKey]:myValue}))

Categories

Resources