I have an object named data and on console of data, I did :
var data={
countrycode: "376",
dob: "2017-05-24",
email: "bsbsbs#gmail.com",
firstName: "ggsgsggsg",
gender: "male",
phoneNumbers: "88888888888888888",
surName: "hshshhshs"
}
let countrycode,iso2,number,firstName,surName,dob,gender,email;
countrycode=data.countrycode;
number=data.phoneNumbers;
firstName=data.firstName;
surName=data.surName;
email:data.email;
gender:data.gender;
dob:data.dob;
console.log('email is',email);
console.log('number is ',number);
console.log('gender is',gender);
console.log('surName is',surName);
console.log('dob is',dob);
console.log('firstName is',firstName);
What I see that as i console log the values , i get undefined for email,gender and dob and for rest of the others , i get the correct values .
What can be the reason this and why does this happen ?
You have to actually use = as an assignment operator in the example you gave, colons are for object property assignment.
email = data.email;
gender = data.gender;
dob = data.dob;
Use = for assignment, not : (which is reserved for assigning values to an Object's property).
It would be
email=data.email;
Not
email:data.email;
var data={
countrycode: "376",
dob: "2017-05-24",
email: "bsbsbs#gmail.com",
firstName: "ggsgsggsg",
gender: "male",
phoneNumbers: "88888888888888888",
surName: "hshshhshs"
}
let countrycode,iso2,number,firstName,surName,dob,gender,email;
countrycode=data.countrycode;
number=data.phoneNumbers;
firstName=data.firstName;
surName=data.surName;
email=data.email;
gender=data.gender;
dob=data.dob;
console.log('email is ',email);
console.log('number is ',number);
console.log('gender is ',gender);
console.log('surName is ',surName);
console.log('dob is ',dob);
console.log('firstName is ',firstName);
Related
Hey guys I have the following setback, I am trying to write on empty values of an object already filled another value so I can use them later, I have defined the object and I am trying to reassign the values with the new one after I receive them from API call, the problem is that the object that I am receiving is the same as the one from the API call and I don't need that, I am in need of the same structure that I have created, for this to happen I am using useState. Could you point me in the right direction?
That is my code and after it will be the result I am always getting
The initial state of deliveryAddressInput before API call
const [deliveryAddressInput, setDeliveryAddressInput] = useState<NewAddressDetails>({});
The result after the call of the API is setDeliveryAddressInput(adress):
BuildingName: "18A"
BuildingNumber: ""
City: "Houghton Le Spring"
Line1: "18A Nesham Place"
Line2: ""
Line3: ""
PostalCode: "DH5 8AG"
Initial state of the mapped object from the top one:
const [newAddress, setNewAddress] = useState<{}>({
firstName: firstNameInput,
lastName: lastNameInput,
houseNo: deliveryAddressInput.BuildingNumber,
houseName: deliveryAddressInput.BuildingName,
street: deliveryAddressInput.Line1,
addressLine2: deliveryAddressInput.Line2,
town: deliveryAddressInput.City,
postCode: deliveryAddressInput.PostalCode,
});
The useState function:
useEffect(() => {
setTimeout(() => {
setNewAddress(deliveryAddressInput);
});
}, [deliveryAddressInput]);
And the final result is the same as the call from the API:
BuildingName: "18A"
BuildingNumber: ""
City: "Houghton Le Spring"
Line1: "18A Nesham Place"
Line2: ""
Line3: ""
PostalCode: "DH5 8AG"
I am trying to receive the following which is mapping the values of the API call to the new object keys:
firstName: "Jane",
lastName: "Smith",
houseNo: "",
houseName: "18A",
street: 18A Nesham Place,
addressLine2: "",
town: "Houghton Le Spring",
postCode: "DH5 8AG",
I suppose you should convert one object into another:
const convertDeliveryInputIntoAddress = (
deliveryAddressInput, firstName, lastName
) => {
const {BuildingName, BuildingNumber, City, Line1, PostalCode} = deliveryAddressInput;
const firstLineSplitted = Line1.split(' ');
const houseName = firstLineSplitted[0];
const street = firstLineSplitted.slice(1).join(' ');
return {firstName, lastName, houseNo: buildingNumber, houseName, street, town: City, postCode: PostalCode};
}
...
setNewAddress(convertDeliveryInputIntoAddress(deliveryAddressInput, firstName, lastName));
Following code outputs {name: "Bob", surname: "Smith"} and it works fine. I want to know can I make it shorter.
((person = { name: 'Bob', surname: 'Smith', age: 22, }) => {
const {
name, // (a) create variable from deconstructing
surname,
} = person;
return {
name, // (b) reuse variable as new object parameter name (and value)
surname
}
})();
Can I somehow merge object deconstruction to variables (a) with returning a new object with Object Property Value shorthand (b)?
I use here shorthand but then its purpose is defeated by the need to manually re-use parameters. I want to mention the name or surname word in my function once not twice...
Destructure person in the function's declaration:
const result = (({ name, surname } = { name: 'Bob', surname: 'Smith', age: 22, }) => ({
name, // (b) reuse variable as new object parameter name (and value)
surname
}))();
console.log(result);
You can not mention it at all
((person = { name: 'Bob', surname: 'Smith', age: 22, }) => {
const {age,...ans} = person;
return ans
})()
I have the following object to which I wish to have a conditional property:
{ name: this.username, DOB: new Date(this.inputDate)}
Say, I wish to add a third property called gender if the user has specified their gender. What would the proper syntax for the following be:
{ name: this.username, DOB: new Date(this.inputDate), if(this.userGender) gender: this.userGender}
P.S. I do not wish to have the gender property in my object if there is no value along with it. So how can I only create the property if the condition is satisfied?
Ideally, you would just add the appropriate property as a second action after declaring your object. So something like:
const myObj = {
name: this.username,
DOB: new Date(this.inputDate),
}
if(this.userGender) myObj.gender = this.userGender;
However, sometimes it's nice to declare an "optional" property inline with the rest of them, in which case you can use object spread to get the effect you're looking for:
const myObj = {
name: this.username,
DOB: new Date(this.inputDate),
...this.userGender
? { gender: this.userGender }
: {}
}
it can be done like this too, more clean and readable.
const myObj = {
name: this.username,
DOB: new Date(this.inputDate),
...(this.userGender && { gender : this.userGender })
}
Try this
let userObj = { name: this.username, DOB: new Date(this.inputDate) }
if(this.userGender)
userObj[:gender] = this.userGender;
I have JSON, which looks like this:
{ '-KiJz4D0pGYE35HPP-E4': { code: '211', lastname: 'Smith', firstname: 'John' } }
I do know that it will always look this way, ONLY one child in any case. How could I get value '211' stored under code key. Right now I have ugly code with foreach, it even works, but... Why would I use foreach at all if I know there is only one child?
Thanks a lot!
DEMO
var jsonObj = {
'-KiJz4D0pGYE35HPP-E4': {
code: '211',
lastname: 'Smith',
firstname: 'John'
}
};
var objKey = Object.keys(jsonObj); // return the object key
var innerObject = jsonObj[objKey]; // return the inner object
var code = innerObject.code; // return the code property value.
console.log(code);
USe Object.keys(obj)[0] to get the first key and then you can get the key code's value from it like
var obj = { '-KiJz4D0pGYE35HPP-E4': { code: '211', lastname: 'Smith', firstname: 'John' } }
console.log(obj[Object.keys(obj)[0]]['code']);
If
var data = { '-KiJz4D0pGYE35HPP-E4': { code: '211', lastname: 'Smith', firstname: 'John' } };
Then
data[Object.keys(data)[0]] will return your inner object
{ code: '211', lastname: 'Smith', firstname: 'John' }
So you easily get code value by data[Object.keys(data)[0]].code
Object.values({ '-KiJz4D0pGYE35HPP-E4': { code: '211', lastname: 'Smith', firstname: 'John' } })[0].code;
Simply get the first value of the outer Object...
I'm trying to figure out how to make this work:
var emails = $("#register_Email").val();
var split = emails.split("#");
var username = split[0];
var first = $("#register_FirstName").val();
var last = $("#register_LastName").val();
var phone = $("#register_PhoneNumber").val();
ref2.set({
username: {
active: true,
email: emails,
first_name: first,
last_name: last,
phone: phone,
role: "technician"
}
});
I can get all of the variables but the problem is I want to pass it into the set() method as seen above. The name of the whole node should be the value of the username, not the word username.
I've tried using literals in this way:
ref2.set({
[username]: {
active: true,
email: emails,
first_name: first,
last_name: last,
phone: phone,
role: "technician"
}
});
But that doesn't work. It just crashes saying I can't pass [ or ] as variable names.
Can't do it with object literal. But you can remember that foo.bar is equivalent to foo['bar'], and write this instead:
var opts = {};
opts[username] = {
active: true,
email: emails,
first_name: first,
last_name: last,
phone: phone,
role: "technician"
};
ref2.set(opts);