I use an ajax request to return an json encode.
Now the return data is as follows:
{"24": {"24":["205", "22", "1", "1", "0", "0"]}};
Which im trying to add to :
///global set
var data = {"24":{"16":["172","22","1","1","0","0"],"15":["160","22","1","1","0","0"]}};
The problem is – my attempt is not adding to the variable. This is my script:
var result = {24: {24:[205, 22, 1, 1, 0, 0]}}; //return data test
var obj = {}
for ( var key in result ){
if ( result.hasOwnProperty( key ) ) {
// If the key already exists
if ( data[ key ] === result[ key ] ) {
// Empty the temporary object
obj = {}
// Loop through the subkeys
for ( var subkey in result[ key ] ) {
if ( result[ key ].hasOwnProperty( [ subkey ] ) ) {
// Fill in the temporary object
obj[ subkey ] = result[ key ][ subkey ]
}
}
// Add the new object to the original object
data[ key ] = obj
}
// If the key doesn't exist, do it normally
else {
data[ key ] = result[ key ]
}
}
}
obj = null
//show change
console.log(data);
I did a check on data after this code runs, and there is no new added data. Can any one see the mistake where / why its not inserting the data?
Try this:
Demo: http://jsfiddle.net/363uy/