function x({ name = 'abc' }) {
console.log(name);
}
x({ name: null })
How to provide default value for above case which handles all falsey value for name? I was expecting that when name: null will be replaced by abc but apparently not the case? It's only working in the below case
function x({ name = 'abc' }) {
console.log(name);
}
x({ })
Wondering if it's possible to provide default value for all falsey value? Of course we can check for the condition explicitly but wondering if there is any alternatives
Default parameter values are only applied when the value is undefined. You'd have to implement such logic yourself.
function x({name}) {
if (!name) {
name = 'abc';
}
console.log(name);
}
x({ name: null })
null is intentional, undefined is not.
In other words, providing null means the value is null, so 'abc' will not be set as the default value.
This should do the trick:
function x({ name }) {
!(!!name) && (name = 'abc');
console.log(name);
}
x({ name: null })
How does redefining suits you in case, when the value was null?
function x({ name }) {
name = name ? name : 'abc';
console.log(name);
}
x({ name: null });
x({ name: 'asd' });
Related
My object
object1 ={
name: xxx,
Id: 212
}
I need a output like:
{
212:xxx
}
Can anyone help me to do it?
for(var key in object1) {
if(object1.hasOwnProperty(key))
{
console.log( eachPeriod[key]);
}
}
You can set the key of the object with a variable enclosed with brackets as [Id]. The convert() function takes the object as an argument and assigns the value of name and Id to the corresponding variables by destructuring. Then, use those variables to construct the object.
const obj = {
name: 'xxx',
Id: 212
};
function convert ({ name, Id }) {
return {
[Id]: name
};
}
console.log(convert(obj));
function addPropertyToProduct(product, property, value) {
let tryThis = property;
product.tryThis = value;
return product;
}
The argument 'product' will be an object that looks like this:
{ type: 'Terminator 2: Judgement Day', price: '£6.99', quantity: 1 }
Given a 'property' as an argument, as well as its corresponding value, update the 'product' to include this new information. Then return the updated 'product'.
E.g. if given the 'property' 'length' and the value '2h 36m', your function should return
{ type: 'Terminator 2: Judgement Day', price: '£6.99', quantity: 1, length: '2h 36m' }
This is the answer I'm receiving:
**+ expected** *- actual*
{
**+ "length": "2h 36m"**
"price": "£6.99"
"quantity": 1
*- "tryThis": "2h 36m"*
"type": "Terminator 2: Judgement Day"
}
This returns the value correctly but names the key as the variable name, not the argument itself?
Use square brackets []:
function addPropertyToProduct(product, property, value) {
let tryThis = property;
product[tryThis] = value;
return product;
}
For more info, check: MDN Computed Property Names
function addProperty(obj,propertyName,value){
obj.propertyName = value;
return obj;
}
This function will simply create a property dynamically and add value to it.
hope it helped.
NB: you can also use the spread operator instead of assigning 'product' to a new variable inside your function. But, yes in general, "Computed Property Names" is the key as Alvaro is saying above:)
function addPropertyToProduct(product, property, value) {
return {
...product,
[property]: value
}
};
I have an object like below. Sometimes it will contain status sometimes not. I want to convert status value format into Number when it in object.
var obj={
name: 'abc',
status: '1',
updated_time: {
'$gt': 2019-11-03T00:00:00.000Z,
'$lt': 2019-11-03T15:23:55.838Z
}
}
i've tried as below but it's not converted;
if(obj.status){
parseInt(obj.status)
}
console.log("$$$$$$$$$$$$$$$$",obj)
console.log print
$$$$$$$$$$$$$$$${
name: 'abc',
status: '1',
updated_time:
{ '$gt': 2019-11-03T00:00:00.000Z,
'$lt': 2019-11-03T15:23:55.838Z }
}
You need to assign back parsed value to object.status. parseInt method just evaluate the value it doesn't changes value by itself in object. you need to change value manually on object.
var obj = {
name: 'abc',
status: '1',
updated_time: {
'$gt': '2019-11-03T00:00:00.000Z',
'$lt': '2019-11-03T15:23:55.838Z'
}
}
if (obj.status) {
obj.status = parseInt(obj.status)
}
console.log("$$$$$$$$$$$$$$$$", obj)
This is just a simple mistake mate. You haven't assigned the value.
You can do that by :
if(obj.status){
obj.status = parseInt(obj.status); //obj.status is being reassigned
}
Hope it helps :)
parseInt returns a value and you need to assign this value to object.status property:
if(obj.status){
obj.status = parseInt(obj.status)
}
I am working on an online class, for which we have to build a list tracker application using react. I have had issues when trying to set the state of one of my classes. In this state I have a variable which is equal to an object. I am attempting to variably set both the name and value of this object, however I was uncertain on how to set the name variably.
let name = this.props.idName;
this.setState((prevState) => {
return {
newItem: { name: item}
};
}, () => {
this.props.addItem(this.state)
});
As you can see, I try to set the key in this object equal to the variable name, however this just sets it to the value of name rather than the value of the the name variable.
You can try this. Just enclose the name in square brackets. More on this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Computed_property_names.
let name = this.props.idName;
this.setState((prevState) => {
return {
newItem: { [name]: item}
};
}, () => {
this.props.addItem(this.state)
});
var k = "key";
var obj = {[k]: "VALUE"}
console.log(obj);
You can regard objects as dictionaries:
$ node
> let name = 'Nyan'
undefined
> let omg = { newItem: {} }
undefined
> omg
{ newItem: {} }
> omg['newItem'][name] = 'item' // note the quotes in this statement
'item'
> omg
{ newItem: { Nyan: 'item' } }
I accidentally missing this keyword inside setter, getter methods. It leads to some weird bugs: (tested with Chrome, Firefox)
Case 1:
let user = {
name: "John",
set fullName(value) {
name = value;
},
get fullName() {
return name;
}
};
user.fullName // ""
user.fullName = "Batman"
user.fullName // "Batman"
user.name // "John"
Why is property name still "John" ? Where did "Batman" come from?
Case 2: change variable name of the above code, something happens:
let user = {
anythingButName: "John",
set fullName(value) {
anythingButName = value;
},
get fullName() {
return anythingButName;
}
user.fullName // anythingButName is not defined at Object.get fullName [as fullName]...
};
Can't use any name but the word name for the variable in above code. I don't know why?
Both cases are equal. What happens:
let user = {
name: "John",
set fullName(value) {
name = value;//sets window.name to *value*
},
get fullName() {
return name;//returns window.name
}
};
console.log(
user.fullName,// window.name is "" by default
window.name,
user.fullName = "Batman", //=> window.name
window.name,
user.fullName, // "Batman" ==window.name
user.name // "John" //what you really wanted
);
It works ( not really ) just with name as window.name is a default property and therefore set to "" at the beginning.
You can check in your second case:
console.log(
user.fullName, // undefined yet
user.fullName="test",
user.fullName // test
);