1. 程式人生 > >物件的鍵值對

物件的鍵值對

var obj={"one":"I am 1","two":"I am 2"};

var arr = Object.getOwnPropertyNames(obj); //arr=["one", "two"]

var arr2 = Object.keys(obj);//arr=["one", "two"]

var valArr = arr.map(function(i){return obj[i]});//["I am 1", "I am 2"]

var valArr2 = arr2.map(function(i){return

 obj[i]});//["I am 1", "I am 2"]

//或者2,4和3,5各自合併成一條語句

var valArr22 = Object.keys(obj).map(function(i){return obj[i]});//["I am 1", "I am 2"]