Issue getting my objects properties [duplicate] - javascript

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 4 years ago.
Hi guys I have just started learning to code and I have hit a road block in accessing the properties of the object I have created.
Here is my object.
var restaurantOrder = {
"my entree": "cheeseburger",
"my side": "fries",
"the drink": "water"
};
I would like to get the value of entree however nothing I am trying seems to work :(
Here is what I have tried.
var entreeValue = restaurantOrder.my entree;
var entreeValue = restaurantOrder[my entree];
var entreeValue = restaurantOrder.[my entree];
var entreeValue = restaurantOrder.["my entree"];
None of the above lines work :( Thank you for your help.

Because the properties of your restaurantOrder object have spaces, you will need to use [] to access them. You cannot use . like you can with a property name that is a single word.
Also, because of the spaces you will need to enclose the property name with quotes, so either:
var entreeValue = restaurantOrder["my entree"];
or
var entreeValue = restaurantOrder['my entree'];
will work.

Related

I want toarray in JavaScript? Where is it my fault? [duplicate]

This question already has answers here:
Object property name as number
(6 answers)
Closed 4 years ago.
I add This code to my page and I want to complete city when user tap a zipcode
and my alert dosen't show
var obj = {
"01400": "ABERGEMENT-CLÉMENCIAT",
"01640": "ABERGEMENT-DE-VAREY",
"01500": "AMBÉRIEU-EN-BUGEY",
"01330": "AMBÉRIEUX-EN-DOMBES",
"01300": "AMBLÉON",
"01500": "AMBRONAY",
"01500": "AMBUTRIX",
"01300": "ANDERT-ET-CONDON",
"01350": "ANGLEFORT",
"01100": "APREMONT",
"01110": "ARANC",
"01230": "ARANDAS",
"01100": "ARBENT",
"01300": "ARBIGNIEU",
"01190": "ARBIGNY"
};
var myVariable = obj .01400;
alert(myVariable);
Firstly, there is no key named 97433 in your object
Secondly, even if there was you cannot use property accesors with object keys which begin with a number. You need to use bracket notation.
Lastly, use console.log() for debugging as alert() coerces types and blocks the UI logic.
var myVariable = obj['97433'];
console.log(myVariable);

How do I use a variable to find an element in an array? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I'd like to take some user input and use it to find a certain object in an array. But when I try to do it with the code below, I get an undefined error. What am I doing wrong?
function findNextLevel() {
var currentLevel = parseFloat(document.getElementById("currentLevel").value);
var xpForLevel = trainerLevels.currentLevel;
document.getElementById("result01").innerHTML = xpForLevel;
}
I'm assuming that trainerLevels is an array and currentLevel is an index. If so, the way to access an element in an array at a certain index is to use brackets like so. Otherwise, could you provide more details in your question?
var xpForLevel = trainerLevels[currentLevel];
If this is the answer that you were looking for, then may I recommend that you use the parseInt rather than the parseFloat function for getting the index? And also, since it is user input, you may want to check that currentLevel is in the correct range as well.

Assign dynamic index in array of object in Angularjs [duplicate]

This question already has answers here:
Variable as the property name in a JavaScript object literal? [duplicate]
(3 answers)
Closed 6 years ago.
If I have array of object in such a way.
data[0].name= 'Prashant Shukla';
$scope.getColumn[i].Field = 'name';
Using above I want to assign Prashant in $scope.getColumn[i].value in dynamic way. I have try like this
$scope.getColumn[i].value = data[0].+$scope.getColumn[i].Field;
but this give me Uncaught SyntaxError: Unexpected token +
I can get Prashant Shukla by data[0].name but How can I get exactly same by using $scope.getColumn[i].Field in place of name index ?
how can I resolve this error and assign value dynamically?
thanks ahead.
you can use as like as given below:
data[0].name= 'Prashant Shukla';
$scope.getColumn[i].Field = 'name';
console.log( data[0][ $scope.getColumn[i].Field ] ); // 'Prashant Shukla'
$scope.getColumn[i].value = data[0].+$scope.getColumn[i].Field;
Why you have . after the data[0]?
It should be data[0].name +$scope.getColumn[i].Field;

How to set json value by using rewire module in node.js [duplicate]

This question already has answers here:
How can I add a key/value pair to a JavaScript object?
(26 answers)
Closed 7 years ago.
//filejson.json
{
"body":
{
"name":"abc"
}
}
//mainFile.js
var readJson = require("/filejson.json")
var req=clone(readJson.body);
I want to change the name of JSON value without changing .json file. I need to set name value using rewire module. Can you help me how to set key value pair dynamically. Is it possible to set like this. Please help me Thanks in advance.
var readJson = require("/filejson.json")
readJson.name = "defg";
Require just use the file, no changes will occur to the file.

Replace function not replacing [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 8 years ago.
I followed some documentation to use the JavaScript replace function and it's not changing anything. No errors are thrown. Any idea what I'm doing wrong? The variable is retrieved from XML - maybe it needs to be cast as a string or something?
for (var i = 0, iln = projects.length; i < iln; i++){
var thumb = projects[i].get('thumb');
thumb.replace("200.jpg", "640.jpg");
console.log(thumb) //200.jpg not replaced
}
The full thumb value should look like this:
http://b.vimeocdn.com/ts/160/895/160895498_200.jpg
Is there a better way to find and replace things?
Assign the value back into thumb.
thumb = thumb.replace("200.jpg", "640.jpg");
Try:
thumb = thumb.replace("200.jpg", "640.jpg");

Categories

Resources