可以使用 ES6 中的 Array.prototype.map() 方法和 Array.prototype.join() 方法来实现:

const arr = [{id: 1}, {id: 2}, {id: 3}];
const ids = arr.map(item => item.id).join(',');
console.log(ids); // '1,2,3'

首先使用 map() 方法将每个对象的 id 属性提取出来,形成一个新的数组,然后使用 join() 方法将这个数组转换成以逗号分隔的字符串。

ES6 提取数组对象 ID 并转换为逗号分隔字符串

原文地址: https://www.cveoy.top/t/topic/ozp3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录