javascript - Vue js get first value of data model - Stack Overflow

Id like to get the first returned value of data.countrys model as below :...el ...etc..data: {countrys:

Id like to get the first returned value of data.countrys model as below :

...el ...etc..

 data: {

    countrys: [
      {KE : 'KENYA'},
      {USA : 'USA'}

  ] ,


  },
... ready...etc..

How can i get the value 'KE' in my method ? below is code am using in my method .

console.log(vm.$data.$children[0].countrys);

Id like to get the first returned value of data.countrys model as below :

...el ...etc..

 data: {

    countrys: [
      {KE : 'KENYA'},
      {USA : 'USA'}

  ] ,


  },
... ready...etc..

How can i get the value 'KE' in my method ? below is code am using in my method .

console.log(vm.$data.$children[0].countrys);

Share Improve this question asked Mar 1, 2016 at 14:33 Jimmy Obonyo AborJimmy Obonyo Abor 7,89510 gold badges48 silver badges71 bronze badges 1
  • 1 (Totally unrelated, but the plural of "country" is "countries".) You'd continue to write normal JS, reach into the countries array, and get the key. It's not as easy as it should be, e.g., each country could have name and code keys, which would make it much simpler. – Dave Newton Commented Mar 1, 2016 at 14:37
Add a ment  | 

2 Answers 2

Reset to default 2

Since 'KE' is a key, you can use Object.keys function on the object within the 'countrys' array:

Object.keys(vm.data.countrys[0])[0];

vm.data.countrys[0] will return the {KE : 'KENYA'} object, and then Object.keys will return an array of keys of that object.

So... data is object, countrys - array of objects, then result is

var country = "KE";
data.countrys[0][country] 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744156983a4560907.html

相关推荐

  • javascript - Vue js get first value of data model - Stack Overflow

    Id like to get the first returned value of data.countrys model as below :...el ...etc..data: {countrys:

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信