0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【JavaScript関数ドリル】初級編のzipObject関数の実装アウトプット

Last updated at Posted at 2022-06-05

zipObject関数の課題内容

_.zipObject関数を自分で実装する内容です。
https://lodash.com/docs/4.17.15#zipObject

「課題内容」/「解説動画」/「解答例」を確認したい場合は、以下リンク先のページを参照。

課題に取り組む前の状態

  • 1ヶ月以上前に課題の動画を1度見た
  • オブジェクトの操作についてあまり覚えていない状態
  • オブジェクトの操作についてリファレンスを調べる必要がある

課題に取り組んだ後の状態

  • 答えを見なくてもだいたい実装できたが、代入方法について思い出せず、リファレンスを調べる必要があった
    https://www.w3schools.com/js/js_objects.asp
  • 解答例の引数が、propsだけではなく、props = [] のようになっている

zipObject関数の実装コード(答えを見る前)

function zioObject(props,values) {
    const newObj = { };
    props.forEach((prop, index) => {
        newObj[prop] = values[index];
    });
    return newObj;
}

console.log(zioObject(['a','b'],[1,2]));

zipObject関数の実装コード(解答例)


function zipObject(props = [], values = []) {
    const zippedObject = {};
    for(let i = 0; i < props.length; i++) {
      const prop = props[i];
      const value = values[i];
      zippedObject[prop] = value;
    }
  
    return zippedObject;
  }
  
  console.log( zipObject(['a', 'b'], [1, 2]) );
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?