This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 4 years ago.
I am making a shopping cart via javascript/jQuery and I am using localStorage. However, I am running into a problem setting the key value of an object. I created this function, but it has not been successful:
function update_aantal(e){
var value = $(e).parent().find(".form-aantal-val");
var size = $(e).closest('td').attr('class');
console.log(size);
valuepush = {
size: value.val()
};
var cart = JSON.parse(localStorage.getItem("src"));
$.extend(cart[0], valuepuch );
localStorage.setItem("src",JSON.stringify(cart));
}
The console.log(size) shows the correct value that I want to change/add into the localStorage but the size in the object valuepush says "size" when the function is complete and the value is added.
How can I pass the size as an object so that the value of size is stored?
var valuepush = {};
valuepush[size] = value.val();
Related
This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed last year.
var i=0;
var mainObj={};
while(i<2){
var obj={};
obj.x ="data";
obj.y= "data";
mainObj.i=obj
i=i+1;
}
console.log(JSON.stringify(mainObj));
output :- {"j":{"x":"data"."y":"data"}}
{"j":{"x":"data"."y":"data"}}
Expected Output : {0:{"x":"data"."y":"data"},1:{"x":"data"."y":"data"}}
how to achieve this expected output i need the same output key should be number either in string format or number format
Try mainObj[i] = obj instead of mainObj.i = obj to use a computed key.
This question already has answers here:
Push is overwriting previous data in array
(2 answers)
Closed 2 years ago.
I am using an angularjs foreach and want to find data objects to load into another objects array.
When doing this, it seems like I am adding the variable and all the added objects change to the last variable values.
var p = {};
angular.forEach(d, function(personnel, index){
if(wo.doc.job === personnel.job){
p["EmployeeID"] = personnel.EmployeeID;
p["Number"] = personnel.Number;
wo.doc.personnel.push(p);
console.log(personnel);
}
});
If this finds to 2 employees for a job, they are added and as i watch the wo.doc object after the second object is added the 2 added objects are the same as the last object.
Make a new object in the loop.
angular.forEach(d, function(personnel, index){
if(wo.doc.WorkOrderDetailID === personnel.PrimeWorkOrderNum){
var p = {EmployeeID: personnel.EmployeeID, Number: personnel.Number};
wo.doc.personnel.push(p);
}
});
This question already has answers here:
How do I concatenate a string with a variable?
(5 answers)
Closed 3 years ago.
I need to assign a time to a profile value from our database, but I don't know how to seperate the values.
It is the following code:
var pauze_datum = new Date("2020-01-27T10:33:00");
The time should be assigned to var Temporary_Date which is a filled in date in a form. How do you seperate the letter e and T?
var pauze_datum = new Date("Temporary_Date T10:33:00");
You can use either regular concatenation:
var value = "2020-01-27";
var pauze_datum = new Date(value + "T10:33:00");
or template literals:
var value = "2020-01-27";
var pauze_datum = `${value}T10:33:00`;
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);
This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 5 years ago.
Say i have object like
var a = {"user":
{'average':
{'score':4
}
}
}
How can I read object value using its keys
Say I have user Object with me and have key "average.score" can I get the value directly?
a.user["average.score"];
//Coming as undefined
a.user["average"]["score"]
// Working as expected : 4
I have the key of "average.score" all together with me want to get the value of score how can I do it directly without splitting the key.
Use a.user["average"].score
var a = {"user":
{'average':
{'score':4
}
}
}
console.log(a.user["average"].score);