js配列map()の基本的な使い方

3626 ワード

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"}
// ]