简体   繁体   中英

how to set values dynamically to javascript var

i've a requirement to set the values dynamically into js var, which i will use it to set to sapui5 model .

but i'm unable to do that. http://jsbin.com/fubobuvoqi/1/edit?html,console here, i'm unable to set next value into the array:

data.a.b.c[1].c1 = "234";
console.log(data.a.b.c[1].c1);

is it possible do that way, or its completely wrong way of doing it.. what is best way of doing this?

i want to grow my c array dynamically.

You cannot assing it, cause it's really doesn't exist yet (undefined), but you can create it using push() try to change

data.a.b.c[1].c1 = "234";

to

data.a.b.c.push({c1: "234"});

this will add one more element to array and make you var look like

  c : [
    {
      c1 : "987",
      c2 : 987,
      c3 : "987.00"
    },
    {
      c1 : "234"
    }
  ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM