js 数组 map() 基本用法

map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。

var data = [3, 4, 5, 6];

var Squares = data.map(function (item) {
  return item * item;
});
console.log(Squares);
// [9, 16, 25, 36]
var users = [
  {name: "张含韵", "email": "zhang@email.com"},
  {name: "江一燕",   "email": "jiang@email.com"},
  {name: "李小璐",  "email": "li@email.com"}
];

var emails = users.map(function (user) { return user.email; });
console.log(emails);
//["zhang@email.com", "jiang@email.com", "li@email.com"]
const configs = {
  umdDev: {
    format: 'umd',
    env: 'development'
  },
  umdProd: {
    format: 'umd',
    env: 'production'
  },
  commonjs: {
    format: 'cjs'
  },
  esm: {
    format: 'es'
  }
}
var obj = Object.keys(configs);
var m = obj.map(key => configs[key]);
console.log(obj);//["umdDev", "umdProd", "commonjs", "esm"]

console.log(m);

// [
//   {format: "umd", env: "development"},
//   {format: "umd", env: "production"},
//   {format: "cjs"},
//   {format: "es"}
// ]

你可能感兴趣的:(js 数组 map() 基本用法)