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関数ドリル】を毎日やる【勉強用】

Posted at

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

##zipObject関数の課題内容

##zipObject関数に取り組む前の状態

・少し調べればすぐにできそう
・学習のメインのFrontHacksでは非同期処理まで進んでいるので簡単にできるはず

##zipObject関数に取り組んだ後の状態

・これまでの学習の復習(アウトプット)が全く足りていないことを痛感した
・初日から実装できずに終わるのが嫌で、かなり時間をかけてしまった
・console.log内で関数を実行すれば良いものをわざわざ変数に代入して表示してしまった
・白紙から実装したことで少し自信がついた

##zipObject関数の実装コード

const zipObject = (props = [], values = []) => {
    const zippedObj = {};
    for (let i = 0; i < props.length; i++) {
        const prop = props[i];
        const val = values[i];
        zippedObj[prop] = val;
    }
    return zippedObj;
};

const result = zipObject(['a', 'b'], [1, 2]);
console.log(result);
// => { 'a': 1, 'b': 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]) );
// => { 'a': 1, 'b': 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?