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 3 years have passed since last update.

JavaScript【基本文法】~Part3~

Posted at

#初めに
今回はJavaScriptの中で、自分が大事だと思った配列メソッドについて書いていきたいと思います。
#目次
###・map()
###・filter()
###・findIndex()

##map()
mapメソッドを使う事で新しい配列を作る事が出来る。

入力

const obj = {
    "size": {text: "big"},
    "shape": {text: "square"},
    "num": {text: "many"}
};

const Array = Object.keys(obj).map(key =>{
    let value = obj[key]
    value['id'] = key
    return value
});
console.log(Array);

結果


[
  { text: 'big', id: 'size' },
  { text: 'square', id: 'shape' },
  { text: 'many', id: 'num' }
]

##filter()
filterメソッドを使う事で条件に合う要素を抽出する事が出来る。

入力

const obj = [
    {id: "size", text: "big"},
    {id: "shape", text: "square"},
    {id: "num", text: "many"}
];
const Result = obj.filter(object =>{
    return object.id === "shape"
});
console.log(Result);

結果


[ { id: 'shape', text: 'square' } ]

##findIndex()
findIndexメソッドを使う事で要素の何番目かを知る事が出来る。

入力

const obj = [
    {id: "size", text: "big"},
    {id: "shape", text: "square"},
    {id: "num", text: "many"}
];
const Index = obj.findIndex(object =>{
    return object.id === "shape"
});
console.log(Index);

結果

1

##参考記事
https://www.youtube.com/watch?v=VBuBGqPGALg

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?