1
I have the following function that creates an object:
function createDebit(name, value, paymentDate){
var debit = {"type": name,"value": value,"PaymentDate": paymentDate};
return debit;
}
This object it returns I’ll add. in another object with this function:
var newDebit = createDebit("GVT", "200.00", "10/10/2010");
addValue("Bradesco", "arrayDebits", newDebit);
function addValue(where, element, content){
var execute;
var local = localStorage.getItem(where);
local = JSON.parse(local);
var verifyType = "$.isArray(local."+element+")";
// Verifica se o elemento do objeto destino é um array
if(eval(verifyType)){
// Caso seja, add o conteudo via push
if(typeof(content) == "string"){
execute = "local."+element+".push('"+content+"')";
}else if(typeof(content) == "object"){
execute = "local."+element+".push("+content+")";
}
}else{
execute = "local."+element+"='"+content+"'";
}
console.log(execute);
console.log("Execute now...");
eval(execute);
console.log("Done Execute");
storage_save(where, local);
}
But it always makes a mistake:
jQuery.Deferred Exception: Unexpected Identifier Syntaxerror: Unexpected Identifier
If I try to pass the object via STRING:
function createDebit(name, value, paymentDate){
var debit = '{"type":' + name + ',"value": ' + value + ',"PaymentDate": ' + paymentDate + '}';
return debit;
}
No mistake, but when I read the JSON transforming it into STRING, I see that in reality it is no longer an object, but a STRING:
{"nameCategory":"Bradesco","arrayDebits":["{ "type ": "GVT ", "value ": value, "Paymentdate" : paymentDate}"]}
How can I actually save the object within the array inside another object?
1 - I saw that if (!local[element]) returns true or false, but what exactly does it do? 2 - The result still gives:
{"nameCategory":"Bradesco","arrayDebits":["{\"type\":GVT,\"value\": 200.00,\"PaymentDate\": 10/10/2010}"]}
But I’m thinking it’s because of the methodfunction storage_save(name, json){
 var stringJSON = JSON.stringify(json);
 localStorage.setItem(name, stringJSON);
}
But if I don’t do it like this, he won’t let me.– CarlosM
@Carlosm
1:
is one way to know iflocal. arrayDebits
is already closed or not. If empty you receive the value ofcontent
directly.2:
This could be because of old values you have in thelocalStorage
. Delete these values and "start again". You have to useJSON.stringify
even, localStorage only accepts strings.– Sergio
1 - Intendi! 2 - truth! It worked right! .
– CarlosM