How to add extra property in object [closed] - javascript

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.

Related

Can't get data from JSON status undefined? [closed]

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);

Why am I getting undefined error? [closed]

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
the syntax seems to be correct, I dont get it, here is
error: app.js:73 Uncaught ReferenceError: getBirthyear is not defined
Code:
const person = {
name: 'Tommy',
age: 32,
location: {
state: 'Missouri',
city: 'louisisana',
street: '1 marcia drive',
job: 'web dev'
},
getBirthyear: function() {
return 2018 - this.age;
}
}
let val;
val = getBirthyear();
document.write(val);
You can call it by using person.getBirthday() since it is a property of person

Why is the array empty after filtering? [closed]

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).

Retrieving data with spaces from SQL [closed]

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'
}
};

JS function being called twice? [closed]

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 8 years ago.
Improve this question
I can't seem to find the bug that's making the code run twice, and JSFiddle isn't working for me so I can't double check if it's the editor.
var friends = {};
friends.bill = {
firstName: "Bill",
lastName: "Gates",
number: 9,
address: ["5242", "drank avenue"]
};
friends.steve = {
firstName: "Steve",
lastName: "Jobs",
number: 8,
address: ["3368", "pool lane"]
};
var list = function(person)
{
for (var friendName in person)
console.log(friendName);
};
list(friends);
Output:
bill
steve
bill
steve
I cut and pasted the code you posted into my JavaScript console and the result was:
bill
steve
Somewhere, in your original code, you must be calling the function twice.
Choose a place in the code — in this case I would choose the top of your list function — and put a
debugger;
statement there, and open the JavaScript console.
There will be an option in that debugger to see a stack trace. The stack trace will let you see exactly where a function was called from, so you can see if it is being called from two different points.
If that doesn't work, move the debugger statement to right above this line:
friends.bill = {
and repeat until you find the problem.

Categories

Resources