How to add an object in an array within another object

Asked

Viewed 1,932 times

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 answer

1


Don’t use the eval for this case, he is dangerous. The localStorage is useful but not to trust, and use strings of localStorage in Val is bad practice.

You can do it like this, note how I use it [] for access objects properties dynamically:

function createDebit(name, value, paymentDate) {
  return {
    "type": name,
    "value": value,
    "PaymentDate": paymentDate
  };
}

function addValue(where, element, content) {
  var local = JSON.parse(localStorage.getItem(where));
  if (!local[element]) local[element] = content;
  else local[element].push(content);
  console.log("Done!");
  storage_save(where, local);
}

var newDebit = createDebit("GVT", "200.00", "10/10/2010");
addValue("Bradesco", "arrayDebits", newDebit);

  • 1

    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 method function 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 1: is one way to know if local. arrayDebits is already closed or not. If empty you receive the value of content directly. 2: This could be because of old values you have in the localStorage. Delete these values and "start again". You have to use JSON.stringify even, localStorage only accepts strings.

  • 1

    1 - Intendi! 2 - truth! It worked right! .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.