This question already has answers here:
Self-references in object literals / initializers
(30 answers)
How can a JavaScript object refer to values in itself? [duplicate]
(8 answers)
Closed 5 years ago.
I have this code
var array, key, dc;
$.post("/mailchimp/check_mailchimp_key",
{
store_id: document.getElementsByName('data[store_id]')[0].value,
mailchimp_api_key: document.getElementsByName('data[mailchimp_api_key]')[0].value,
array: mailchimp_api_key.split('-'),
key: array[0],
dc: array[1]
}
store_id and mailchimp_api_key work, but I have problem with others. This way it says mailchimp_api_key is not defined and my goal is to take whatever is stored in mailchimp_api_key and divide it to key and dc.
mailchimp_api_key: document.getElementsByName('data[mailchimp_api_key]')[0].value
… means that when the object has finished being constructed it will have a property called mailchimp_api_key with that value.
mailchimp_api_key.split('-'),
… tries to read a variable called mailchimp_api_key.
There are two problems with this:
A variable is not an object property
The object property doesn't exist yet
Copy the value to a variable before you construct the object.
Use it twice.
var array, key, dc;
var mailchimp_api_key = document.getElementsByName('data[mailchimp_api_key]')[0].value;
$.post("/mailchimp/check_mailchimp_key",
{
store_id: document.getElementsByName('data[store_id]')[0].value,
mailchimp_api_key: mailchimp_api_key,
array: mailchimp_api_key.split('-'),
key: array[0],
dc: array[1]
}
This is not Javascript at all! You can't reference Object keys that you just defined.
var mailchimp_api_key = document.getElementsByName('data[mailchimp_api_key]')[0].value;
var array = mailchimp_api_key.split('-');
var key = array[0];
var dc = array[1];
This way you get all the variables you need, then you might want to pass them in your Ajax call.
Related
This question already has answers here:
Copy array by value
(39 answers)
What is the most efficient way to deep clone an object in JavaScript?
(67 answers)
Closed 5 years ago.
How can an array value or an object be copied to another variable without copying its reference in Javascript so that the copied value and variable can be manipulated independently, without causing any change to the original one?
Array coping by value:
var array = [1,2,3];
var newArr = array.slice();
and Object coping by value:
var obj = {name : "A"};
var new_obj = JSON.stringify(obj);
This question already has answers here:
Copy array by value
(39 answers)
Closed 5 years ago.
function myFunction() {
var a = {};
a.myArray = ["Saab", "Volvo", "BMW"];
var b=a.myArray;
b.push("Chevy");
alert(a.myArray);}
In this above function I have created an object "a" and created an array "myArray". Now I am copying that array in "b" and modifying array "b".But why is it changing a.myArray and how to avoid this?
Because you assigned the reference of that array. So you are actually referring the same array with different variable name.
If you want to seperate arrays
var b = a.slice();
Which gives you a new array with same values of your initial array.
slice()
The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified.
This question already has answers here:
Use a concatenated (dynamic) string as JavaScript object key? [duplicate]
(6 answers)
Closed 8 years ago.
I have a javascript array and a variable. I want to push onto the array an object with the variable as the property name, but it must hold the value of the variable, not the variable as a string.
If I have this code:
array = [];
var x = 10;
array.push({x: y});
The x is stored as a string "x", not a variable that contains the value of var x.
Any help is appreciated.
The property name in an object literal is not evaluated as a variable. You have to assign the property using bracket notation.
var obj = {};
obj[x] = y;
array.push(obj);
This question already has answers here:
JavaScript by reference vs. by value [duplicate]
(4 answers)
Closed 8 years ago.
I have a question about referencing objects in javascript.
Say I have a variable that is some object (lets say json) and it is called objOne - (var objOne = someJSONObject;).
If I go ahead and declare
var objTwo = objOne;
will I have two references to the same Object? Kind of like a c pointer?
To sum it up :
assignements are done by value
you never manipulate objects, only object references
This means that
you'll have two references to the same object (you can check that by changing a property of the object)
when you're passed in a variable the value of a primitive, changing your variable doesn't change other variables
EDIT : as it's a duplicate I'll delete this answer in a minute to allow a proper closing if there's no other answer. Please vote to close.
Yes, objects are passed by reference:
function changeVal(obj){
obj.value = "bar"
}
(function checkRefs(){
var myObject = {
value: "foo"
};
alert(myObject.value);
changeVal(myObject);
alert(myObject.value);
})();
This question already has answers here:
How to create an object property from a variable value in JavaScript? [duplicate]
(9 answers)
Closed 9 years ago.
I have an object:
var obj = {fields : []};
And a variable:
var x = "property_name";
Now how do I use the value in the variable x as the property name for the object that I'm going to push inside the array?
I tried something like the one below, but it takes x literally and uses it as the property name. What I want to use is the value stored in the variable x. Is that possible?
obj.fields.push({x : 'im a value'});
You cannot use the object literal syntax for this purpose. However, you can create a new object and then use the [] syntax - remember, obj.xyz is equivalent to obj['xyz'] - but as you can see with the quotes in the latter, you can use an expression there - such as a variable:
var x = "property_name";
var obj = {};
obj[x] = 'value';
obj.fields.push(obj);