I do not know why I got Uncaught SyntaxError for this code
const relationship2 = {
name = 'zero',
friends: ['nero', 'hero', 'xero'],
logFriends() {
this.friends.forEach(friend => {
console.log(this.name, frined);
});
},
};
Object needs key/value pair which are seprated by :not =
change this
name = 'zero',
to this
name : 'zero',
Related
I have a field object that can contain only one of different properties with an id as a value.
const Field = {
// Field can contain
property1Id: 'someId',
// Or
property2Id: 'someOtherId',
// Or
property3Id: '...'
//...
};
I want to return the property name and it's value. The following works fine but feels a bit long. Anyway to reduce it / be more efficient.
const propertyName = Field.property1Id
? 'Property 1'
: someObject.property2Id
? 'Property 2'
: someObject.property3Id
? 'Property 3'
: 'Other';
const id = Field.object1Id
? Field.property1Id
: Field.property2Id
? Field.property2Id
: Field.property3Id
? Field.property3Id
: null;
console.log(propertyName, id)
Thanks.
If this is what you mean, every property name with property value is returned :)
const Field = {
// Field can contain
property1Id: 'someId',
// Or
property2Id: 'someOtherId',
// Or
property3Id: '...'
//...
}
let entries = Object.entries(Field);
entries.forEach(entry => {
console.log(entry)
})
Best approach
const Field = {
// Field can contain
property1Id: 'someId',
// Or
property2Id: 'someOtherId',
// Or
property3Id: '...'
//...
};
let i = 0;
for (const property in Field) {
i++
console.log('Property ' + i, Field[property]);
}
Output:
"Property 1" "someId"
"Property 2" "someOtherId"
"Property 3" "..."
I have an object like this which I console log like this:
console.log(node.data.target.fields);
file:
en-US: {url: "//images.ctfassets.net/qkwv5aiilkmk/4YsaPFrSxMPKuMVnO/efb6b59a369e4f30105aaea54fb9f62f/aaa.jpeg", details: {…}, fileName: "aaa.jpeg", contentType: "image/jpeg"}
I want to access the url value but It throws an error :
const url = node.data.target.fields.file.['en-US'].url;
How can I access the en-Us variable properly?
Since en-US cannot be used as a variable name (- is an operator, cannot be used as a part of a variable), you cannot use the dot notation. So the only way you can access is using the [] notation.
const url = node.data.target.fields.file['en-US'].url;
The above is the right way of accessing it, else, you can also access this way:
const lang = 'en-US';
const url = node.data.target.fields.file[lang].url;
Two cases example:
const node = {
data: {
target: {
fields: {
file: {
"en-US": {
url: "//images.ctfassets.net/qkwv5aiilkmk/"
}
}
}
}
}
};
(() => {
console.log("Using direct access...");
const url = node.data.target.fields.file['en-US'].url;
console.log(url);
})();
(() => {
console.log("Using a variable...");
const lang = 'en-US';
const url = node.data.target.fields.file[lang].url;
console.log(url);
})();
To add from the given answer just a few guidelines for accessing Object Properties here are some tips that would help you in the future.
1. Dot property accessor
This common way to access the property of an object is the dot property accessor syntax:
expression.identifier
const hero = {
name: 'Batman'
};
// Dot property accessor
hero.name; // => 'Batman'
1.1 Dot property accessor requires identifiers
-The dot property accessor works correctly when the property name is a valid identifier. An identifier in JavaScript contains Unicode letters, $, _, and digits 0..9, but cannot start with a digit.
const weirdObject = {
'prop-3': 'three',
'3': 'three'
};
weirdObject.prop-3; // => NaN
weirdObject.3; // throws SyntaxError: Unexpected number
To access the properties with these special names, use the square brackets property accessor (which is described in the next section):
const weirdObject = {
'prop-3': 'three',
'3': 'three'
};
weirdObject['prop-3']; // => 'three'
weirdObject[3]; // => 'three'
2. Square brackets property accessor:
-expression[expression]
const property = 'name';
const hero = {
name: 'Batman'
};
// Square brackets property accessor:
hero['name']; // => 'Batman'
hero[property]; // => 'Batman'
3. Object destructuring:
const { identifier } = expression;
const hero = {
name: 'Batman'
};
// Object destructuring:
const { name } = hero;name; // => 'Batman'
Note that you can extract as many properties as you’d like:
const { identifier1, identifier2, .., identifierN } = expression;
I have the following array that has been generated by the DocuSign API:
const signers = [
exports {
email: 'email1#gmail.com',
name: 'Test Name One',
recipientId: 'signer_1',
routingOrder: '1'
},
exports {
email: 'email2#gmail.com',
name: 'Test Name Two',
recipientId: 'signer_2',
routingOrder: '2'
},
exports {
email: 'email3#gmail.com',
name: 'Test Name Three',
recipientId: 'signer_3',
routingOrder: '3'
}
]
I need to get the index of the object in this array where the recipientId === 'signer_2' (for example), and have tried the following:
const signerKey = signers.filter(signerObj => {
console.log(signerObj) // returns "exports { ...email, name, etc }"
console.log(Object.keys[signerObj]) // returns undefined
console.log(signerObj.exports.recipientId) // returns undefined
console.log(typeof signerObj) // returns object
return signerObj.recipientId === 'signer_2' // returns undefined
})
How do I deal with finding data within these exports since they're not actual objects?
For some reason when I used JSON.stringify() then JSON.parse I was able to process everything normally:
const stringified = JSON.stringify(signers)
const signersObject = JSON.parse(stringified)
#casenonsensitive recommended using the lo-dash framework, which I haven't looked into yet, but could be a better solution.
I want to populate the "data" array with the data I am receiving from "displayUserInfo". Not surprisingly the code snippet bellow only takes and records the last line of my input.
populateArray = () => {
var index= 0;
let data = [{
name: "",
surname: "",
age: ""
}]
this.state.displayUserInfo.map(currData => {
data.name = currData.givenName;
data.surname = currData.givenSurname;
data.age = currData.givenAge;
index++;
});
console.log(data);
return data;
}
I would like to do somehting like this:
data[index].name = currData.givenName;
But it gives me error:
TypeError: Cannot set property 'name' of undefined
How can I have all the data from the input(Array of objects) mapped correctly to the "data" array? Is this method correct? Any other approaches? Thanks
self.resultList.forEach(function(item, index, enumerable){
console.log(self.resultList);
item.id=11;
item.get('id');
});
the item like this:
if item.id = 11;
the exception like this:
Assertion failed: You must use Ember.set() to access this property (of
[object Object])
so item.get('id') or item.set('id',11)
the exception like this
Uncaught TypeError: Object # has no method 'get'
is this item not the Ember's Object?so what the item is?
could someone tell me how to change the 'itme.id's value..
Thanks a million
You can use the Ember.set(yourObject, propertyName, value); and Ember.get(yourObject, propertyName); to safely set and get properties.
In your case:
self.resultList.forEach(function(item, index, enumerable) {
Ember.set(item, "id", 11);
Ember.get(item, "id");
});
In my case I did it in this way
//In my controller I've defined the array
displayInfoCheckboxes: [
{
turnover: {
label: "Turnover",
value: 1,
propertyName: "turnover"
}
}, {
pl: {
label: "P&L",
value: 1
}
}
]
//and in my handler I passed the full string path to the property in the set method
let displayInfoCheckboxes = this.get('displayInfoCheckboxes');
let controller = this;
displayInfoCheckboxes.forEach(function(items,index) {
for (var key in items) {
controller.set('displayInfoCheckboxes.' + index + '.' + key + '.value', false);
}
})