javascript - 将对象的特定键值数组转换为逗号分隔的字符串

标签 javascript typescript

我们如何将对象的特定键值转换为逗号分隔的字符串,当它是数组时我可以转换,但我的问题是我的数据是对象数组,所以我想将每个 id 转换为字符串数组就像下面的例子一样。

#当前代码 - 只是初步想法

console.log(['a', 'b', 'c'].join(','));

#示例对象

data = [
    {
        "id": 496,
    },
    {
        "id": 381,
    }
]

#预期结果

  '496,381'

最佳答案

您可以简单地map对象数组的项目,以提取其 id 键的值:

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

data.map(item => item.id).join()

关于javascript - 将对象的特定键值数组转换为逗号分隔的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73821903/

相关文章:

javascript - 使用 Angular (2) routerLinkActive 指令进行变形路由

javascript - 如何缓存png,ico,jpeg

javascript - React.useState() 的问题

javascript - ngSrc 更改时 Angular 指令 : rescale images, 更新

javascript - 使用 Firebase,firebase.auth().currentUser 是 null 值,即使它正在被记录

typescript - 将分层 JSON 映射到 TypeScript-KnockoutJS 类型对象

javascript - 如何使此 typescript 映射功能不那么复杂?

javascript - 如何用一行代码获取字符串数组的最后一个元素?

javascript - 使用 remoteSort 在某种商店上触发什么方法?

javascript - 为什么我的 react 表没有更新数据?