Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am trying to get the status of de.zip-code but get a error Cannot read property 'status' of undefined. In my js file I have this
$.getJSON("link", function(json) { let creditcheck = json.de_zip_code.status; and more.
{
de.zip-code: {
status: "RED",
link: "link",
text: "text"
},
glasvezel-availability: {
status: "RED",
link: "link",
text: "text"
}
}
You have to use the same name as your property when trying to access it (creditcheck = json.de_zip_code.status won't work).
If that property contains special characters (. and - here), then you must use the bracket notation instead of dot notation to access the property:
data['de.zip-code'].status
See the doc about property accessors.
const data = {
'de.zip-code': {
status: 'RED',
link: 'link',
text: 'text'
},
'glasvezel-availability': {
status: 'RED',
link: 'link',
text: 'text'
}
};
console.log(data['de.zip-code'].status);
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 26 days ago.
Improve this question
I'm trying to update the object with this field included: [{ name: 'Euler', age: 27, location: 'remote' }]
at the moment the str is: [{ name: 'Euler', age: 27 }] so the location fields needs to be added.
I've done this so far:
function updateRemoteStudents (str) {
str["Location"] = "remote";
console.log (str);
// Your code here
}
result i get is: [{ name: 'Euler', age: 27 }, Location: 'remote' ].
How can i change the function to reflect what i want (sorry new to coding)
[{ name: 'Euler', age: 27, location: 'remote' }]
change to str[0]["Location"] = "remote"; because you are passing an array.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
myObj = {anArray: ["1", "2", "3", "4"]}
if (myObj["anArray"].includes("2")) {
console.log("hey")
}
this throws an error:
TypeError: MyObject.anArray.contains is not a function
So how do I use methods on an array in an object?
any help would be much appreciated
contains() doesn't exist for Array in javascript.
Have a look on includes() which return true or false, or indexOf() which return the position.
W3School - includes()
W3School - indexOf()
Array has no method contains use includes instead
const myObj = { anArray: ['1', '2', '3', '4'] };
if (myObj['anArray'].includes('2')) {
console.log('hey');
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm trying to filter some objects in an array. But when I do it, I just get an empty array.
Code:
let guilds = guildsData.filter((el) => {
return el.owner == 'true';
});
console.log(guilds);
Array:
[
{ owner: false,
permissions: 2146958463,
icon: 'e568d2b87e31358588cb982354628d51',
id: '267920024570691586',
name: 'Hydra' },
{ owner: true,
permissions: 2146958463,
icon: null,
id: '269159705794838529',
name: 'test 2' } ]
(I removed most of the objects, but they all look like this)
The string 'true' is not the same as the boolean constant true.
When one side of == is boolean and the other is something else, the comparison is done after converting that boolean to number. Thus
'true' == true
is carried out as
'true' == 1
Try this:
let guilds = guildsData.filter((el) => {
return el.owner;
});
no need to compare true to 'true' (they are not the same anyway).
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm trying to display custom icons on a map and I'm almost there, I just have a simple issue that I can't find an answer to. There are three types of icon which correspond to data in the database in a column called 'Scarcity'. I have the data in an xml file and the icons display for 'Common' and 'Rare' but don't display for 'Very rare'. How do I capture this data with spaces in js?
The js I'm using for the icons is:
var customIcons = {
Common: {
icon: '../Images/Common.png'},
Rare: {
icon: '../Images/Rare.png'},
Very rare: {
icon: '../Images/veryRare.png'}
};
Thank you!
you will have to put keys in quotes like :
var customIcons = {
'Common': {
'icon': '../Images/Common.png'
},
'Rare': {
'icon': '../Images/Rare.png'
},
'Very rare': {
'icon': '../Images/veryRare.png'
}
};
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I getting error:
ReferenceError: center is not defined
and console marks <!DOCTYPE html>. Why? Without a parameter in the function call everything is fine. All HTML tags are fine, on top I have <!DOCTYPE html>
<button onclick="getRegion(center)" class="btn btn-region">centrum</button>
function getRegion(region) {
var listRegions = {
"center": [ '7', '5' ],
"south": [ '12' ],
"north": [ '11' ],
"west": [ '4' ]
};
var activeRegion = listRegions[region];
//remClass();
for ( var i = 0; i < activeRegion.length; i++ ) {
$('section.bok-map').find( $('.pl' + activeRegion[i]) ).addClass('active-region');
}
}
In the getRegion(center) call, center needs to be quoted because you want a string.
<button onclick="getRegion('center')" class="btn btn-region">centrum</button>
Without the quotes, it is treated as a variable called center which obviously doesn't exist.