.reduce() method reduce() methodを使うことで、arrayの要素から1つの値を計算します。 reduce() method reduces an array into a single value. よく使われるのは、足し算と掛け算です。 const grades = [10, 15, 5]; const sum = grades.reduce((total, cur […]
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 n […]
const tweets = [ { id: 1080777336298049537, message: "Hello Twitter 👋", created_at: "2020-01-03 11:46:00" }, { id: 1080777336298195435, message: "How do you keep track of your notes?", created […]
Array destructuring const dimensions = [20, 5] // create variables const [width, height] = dimensions; // log them console.log(width); //20 console.log(height); //5 [20, 5]という配列から、widthに20、heightに5が格納 […]
足し算の例 const grades = [10, 15, 5]; const sum = grades.reduce((total, current) => { return total + current; }, 0); 掛け算の例 const numbers = [5, 2, 10]; const result = numbers.reduce((total, current) =&g […]
Array of object : const users = [{ id: 1, name: "Sakura Miko" }, { id: 2, name: "Usada Pekora" }]; Convert to DOM : const html = `<ul> ${users.map(user => `<li>${user.name}</l […]
JavaScriptでstring(文字列)をarray(配列)に変換するには、String.split(separator)が使えます。 let apps = "Calculator,Phone,Contacts"; // これは文字列 let appsArray = apps.split(","); // 上の文字列を,で区切って配列にしてね console.log(appsArray […]