0
0

object.map() is not a functionの解決策

Posted at

jsonファイルをスプレッドシートにいれる際、必要データのみを取り出すためにmap()を使用した。

その際に、object.map() is not a funcitonというエラーに悩まされたが解決したので方法をメモしておく。

mapはarray(配列)でなければ操作をできないが、jsonのデータはobjectらしい。

その違いは、{}で囲まれているか、[]で囲まれているかである。

つまり、array型にすれば解決するのである。

したがって、元の記述を以下のようにしていたら

weatear.map(d => {
    add_values.push([d.firstDay, d.lastDay, d.temp]);

以下のように変えればよいのである。

 Object.values(weather).map(d => {
          add_values.push([d.firstDay,d.lastDay,d.temp]);
        });

要するに、

Object.values("ここにオブジェクトデータを入れる").map()

これでエラーを解消できた。

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