How can I make php array into Javascript object code?.
You can try this. This is how you dynamically name object properties and object values. I am assuming you want to build this dynamically where the property names are coming from a source. All you have to do is loop through the values
var treeData = {};
Loop Here
var nameProperty = "propertyName";
var nameValue = "propertyValue";
var childProperty = "child";
var childValue = {};
Try This.
http://jsfiddle.net/zKdMa/1/
Related
I am unable to add a object to object.
i am creating a object with two 2 properties
i want to assign this object to another object
$scope.urlMappings = {};
$scope.Mapping = function() {
var newUrlMappingField = {};
newUrlMappingField.Url1 = '';
newUrlMappingField.Url2 = '';
$scope.urlMappings.push(newUrlMappingField);
and wat key would i need to use to call ng-repeat in html file ? i m using mapping in urlMappings
I am unable to add a object to object.
i am creating a object with two 2 properties
i want to assign this object to another object
and wat key would i need to use to call ng-repeat in html file ? i m using mapping in urlMappings
push() method is used to add items to an array, not an object.
To add items to an object you can use the bracket notation to set the property of the object and assign the new object to it.
$scope.urlMappings = {};
$scope.Mapping = function() {
var newUrlMappingField = {};
newUrlMappingField.Url1 = '';
newUrlMappingField.Url2 = '';
$scope.urlMappings["mapping"] = newUrlMappingField;
}
For the second question, as far as i understand
<div ng-repeat="(key, value) in urlMappings">
<div>{{key}}: {{value.Url1}} {{value.Url2}}</div>
</div>
I have json:
var obj = '{"Form":[],"Provider":[]}';
I push the data with variable value to make dynamic objects:
var pName = 'Tester';
var data = {
pName :["testing"]
};
console.log(obj['Provider'].push(data));
But that adds pName as variable name but not variable value that is Tester, i tried with +pName+ that also not works.
Returns:
{"Form":[],"Provider":[{"pName":["Testing"]}]}
Any help would be appreciated.
You must use [] syntax near the property name.It will evaluate the expression in the [] and returns the value.
See the example.Here the data's property is with the name 'Tester'.
var obj = {"Form":[],"Provider":[]};
var pName = 'Tester';
var data = {
[pName] :["testing"]
};
console.log(data.pName); // undefined
console.log(data.Tester); // OK
obj['Provider'].push(data);
console.log(obj);
How can I use a variable value as an object key?
For example, when adding an object dynamically to a Collection. When I to do it like this:
addToDB(type, account) {
Accounts.insert({type: account});
};
it doesn't work as the key can't be a variable here.
JavaScript object literal don't support dynamic keys.
Instead you can achieve the goal using :
var obj = {};
var key = "some key";
obj[key] = "test";
In your case:
addToDB(type, account) {
var obj = {};
obj[type] = account;
Accounts.insert(obj);
};
More details here:
Creating object with dynamic keys
I'm having trouble with this one.
//Targeting a property of an object using an array
myArray = ["property0","property1","property2","property3"];
myObject = {};
myObject[myArray[0]] = "value0";
//accepts this line as myObject.property0 = "value0";
alert(myObject.myArray[0]); // <-- how can I target this using my Array?
//this fails
// alert(myObject.property0);
//this works
The same way you set it:
alert(myObject[myArray[0]])
If I have:
var myArray = new Array();
myArray['hello'] = value;
How can I change the key 'hello' to something else?
Something like this would work.
var from = 'hello',
to = 'world',
i, value = myArray[from];
for( i in myArray )
if( i == from ) myArray.splice( i, 1 );
myArray[to] = value;
But is there a native function or a better way to do it?
edit:
Due to the lack of associative arrays in js, what I want to do modify the property name of an object as efficiently as possible.
In JavaScript there is no such thing as associative Array. Objects can be used instead:
var myHash = new Object();
or
var myHash = {};
replace can be done like this:
myHash["from"] = "value";
myHash["to"] = myHash["from"];
delete myHash["from"];
but the preferred way to write it:
myHash.from = "value";
myHash.to = myHash.from;
delete myHash.from;
You can't really "change" the property name, but you can always assign a property value to a new name, and then delete the original one.
myArray['world'] = myArray.hello;
delete myArray.hello;
Also, you're working with an Array instance but using it as a simple object; everything you're doing would work just as well with:
var myArray = {};
The "splice()" you're attempting in the code posted won't work, because it's only for the actual integer-indexed array properties, and not the named properties.
That "delete" doesn't really delete a property really doesn't matter. The "undefined" value is what you get when you check an object for a property and there's no such property.