質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

Q&A

解決済

2回答

763閲覧

javascript のfindIndexについて。

21212121

総合スコア61

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

0グッド

0クリップ

投稿2019/11/19 10:33

import axios from 'axios'; import { key, proxy } from '../config'; export default class Recipe { constructor(id) { this.id = id; } async getRecipe() { try { const res = await axios(`${proxy}https://forkify-api.herokuapp.com/api/get?key=aaa${key}&rId=${this.id}`); this.title = res.data.recipe.title; this.author = res.data.recipe.publisher; this.img = res.data.recipe.image_url; this.url = res.data.recipe.source_url; this.ingredients = res.data.recipe.ingredients; } catch (error) { console.log(error); alert('sometihnd went wrong: ('); } } calcTime() { const numIng = this.ingredients.length; const periods = Math.ceil(numIng / 3); this.time = periods * 15; } calcServings() { this.servings = 4; } parseIngredients() { const unitsLong = ['tabelspoons', 'tablspoon', 'ounce', 'ounces', 'teaspoon', 'teaspoons', 'cups', 'pounds']; const unitsShort = ['tbsp', 'tbsp', 'oz', 'oz', 'tsp', 'tsp', 'cup', 'pound']; const newIngredients = this.ingredients.map(el => { // 1) uniform units let ingredient = el.toLowerCase();/*--大文字を小文字に変換する--*/ unitsLong.forEach((unit, i) => { ingredient = ingredient.replace(unit, unitsShort[i]); }); // 2) remove parenthese ingredient = ingredient.replace(/ *([^)]*) */g, ''); // 3) parse ingredients into count , unit and ingredient const arrIng = ingredient.split(' '); const unitIndex = arrIng.findIndex(el2 => unitsShort.includes(el2)); let objIng; if (unitIndex > -1) { // there is a unit // Ex 4 1/2 cups, arrCount is [4, 1/2] // Ex 4 cups ,arrCount is [4] const arrCount = arrIng.slice(0, unitIndex); let count; if (arrCount.length === 1) { count = eval(arrIng[0].replace('-', '+')); } else { count = eval(arrIng.slice(0, unitIndex).join('+')); } objIng = { count, unit: arrIng[unitIndex], ingredient: arrIng.slice(unitIndex + 1).join(' ') }; } else if (parseInt(arrIng[0], 10)) { // console.log(arrIng); // there is NO unit, but 1st element is number objIng = { count: parseInt(arrIng[0], 10), unit: '', ingredient: arrIng.slice(1).join('') } } else if (unitIndex === -1) { // there is not unit and NO number in 1s possition objIng = { count: 1, unit: '', ingredient } } return objIng; }); this.ingredients = newIngredients; } }

findIndexがあまり理解できていないので、ご教授していただければ幸いです。
また、 const unitIndex = arrIng.findIndex(el2 => unitsShort.includes(el2));
ここでいうel2はどこの部分を指しているのですか?
かなり初歩的な質問で大変恐縮なのですが、ご教授いただければ幸いです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

また、 const unitIndex = arrIng.findIndex(el2 => unitsShort.includes(el2));

ここでいうel2はどこの部分を指しているのですか?

arrIngの各要素を指しています。
もう少し詳しく言うとarrIngの要素を1個ずつ取り出してunitsShort.includes(el2)の処理をしています。

投稿2019/11/19 10:50

dice142

総合スコア5158

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

21212121

2019/11/19 11:30

dice142さん ありがとうございます! 無事つながりました!
guest

0

ベストアンサー

Array.findIndexにあるように、配列要素となります。

ご質問の el2 は、arrIng の配列要素ですね。

ラムダ式で書かれた関数を少しだけ展開すると、こうなります。

const unitIndex = arrIng.findIndex( (el2) => { return unitsShort.includes(el2) });

arrIng を更に追いかけていくと、加工前は res.data.recipe.ingredients になります。

投稿2019/11/19 11:04

AkitoshiManabe

総合スコア5432

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

21212121

2019/11/19 11:16

AkitoshiManabe 配列内の要素が指定されたテスト関数を満たす場合、配列内の インデックス を返します。そうでない場合は -1 を返します。 とありますが、’配列内の要素が指定されたテスト関数を満たす場合’は今回の場合はどこに当てはまりますか?
AkitoshiManabe

2019/11/19 11:23 編集

指定されたテスト関数 includes(el2) を満たすと、arrIngの el2 が格納されたインデックスになりますね。 arrIng.indexOf(el2) と考えるとわかりやすいかもしれません
21212121

2019/11/19 11:29

AkitoshiManabe さん ありがとうございます! 無事理解できました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問