子舒的博客

将数组中多个对象的同名属性值取出合并成新数组

业务中需求的方法,接口返回一个数组,里面包含了大量的对象,具有同名的属性名,比较常见。但是需要将其中参数为 name 的属性值全部取出,合并成数组。

const num = [
    {
      id: 1,
      name: 'abc',
    },
    {
      id: 2,
      name: 'xyz',
    }
]
function getFields(arrnum, field) {
    const resnum = [];
    for (let i = 0; i < arrnum.length; ++i)
    resnum.push(arrnum[i][field]);
    return resnum;
}

const result = getFields(num, "name");
console.log(result);             // ['abc', 'xyz']
console.log(result.join(' '));   // "abc xyz"

  • LinuxDo
  • Giscus
  • Twikoo
昵称
邮箱
网址
0/500
  • OωO
  • |´・ω・)ノ
  • ヾ(≧∇≦*)ゝ
  • (☆ω☆)
  • (╯‵□′)╯︵┴─┴
  •  ̄﹃ ̄
  • (/ω\)
  • ∠( ᐛ 」∠)_
  • (๑•̀ㅁ•́ฅ)
  • →_→
  • ୧(๑•̀⌄•́๑)૭
  • ٩(ˊᗜˋ*)و
  • (ノ°ο°)ノ
  • (´இ皿இ`)
  • ⌇●﹏●⌇
  • (ฅ´ω`ฅ)
  • (╯°A°)╯︵○○○
  • φ( ̄∇ ̄o)
  • ヾ(´・ ・`。)ノ"
  • ( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
  • (ó﹏ò。)
  • Σ(っ °Д °;)っ
  • ( ,,´・ω・)ノ"(´っω・`。)
  • ╮(╯▽╰)╭
  • o(*////▽////*)q
  • >﹏<
  • ( ๑´•ω•) "(ㆆᴗㆆ)
  • 😂
  • 😀
  • 😅
  • 😊
  • 🙂
  • 🙃
  • 😌
  • 😍
  • 😘
  • 😜
  • 😝
  • 😏
  • 😒
  • 🙄
  • 😳
  • 😡
  • 😔
  • 😫
  • 😱
  • 😭
  • 💩
  • 👻
  • 🙌
  • 🖕
  • 👍
  • 👫
  • 👬
  • 👭
  • 🌚
  • 🌝
  • 🙈
  • 💊
  • 😶
  • 🙏
  • 🍦
  • 🍉
  • 😣
  • 颜文字
  • Emoji
  • Bilibili
1 条评论
叶开

看不懂 哈哈。::heo:呲牙笑::

子舒. Some rights reserved.

Using for Hugo.