Array of objects
次のarray of objectsを例にします
const tweets = [
{
id: 10512,
message: "Hello Twitter 👋",
stats: {
likes: 41,
retweets: 54
}
},
{
id: 41241,
message: "How do you keep track of your notes?",
stats: {
likes: 14,
retweets: 20
}
}
];
Converting to CSV (comma-separated values)
.map()と.join()を使って、array of objects を CSV stringに変換します
const csv = tweets.map(tweet => tweet.message).join(", ");
console.log(csv); // "Hello Twitter 👋, How do you keep track of your notes?"
.map()を使ってCSVにしたいものを抽出し、.join()で区切り文字を指定します。
Object destructuring
tweets.forEach(tweet => {
const {likes, retweets} = tweet.stats;
console.log(likes, retweets);
});
返ってくるのは
41 54 14 20
コメントを書く