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オブジェクト

Last updated at Posted at 2020-11-07

配列

末尾に要素を追加→push()
末尾の要素を削除→pop()
先頭に要素を追加→unshift()
先頭の要素を削除→shift()
a.splice(変化が開始する位置,削除数)
splice(変化が開始する位置,削除数,追加する要素)

スプレッド構文

const otherScore = [10,20];
const scores = [20,40,30, otherScore];

では、配列の中に配列ができてしまう。

const otherScore = [10,20];
const scores = [20,40,30, ...otherScore];

スプレット構文を使うことで
[20,40,30,10,20]という配列ができる。

forEach

  const scores = [80,90,40,70];

  scores.forEach((score,index)=>{
    console.log(`Score${index}:${score}`);
  });

map

  const prices = [180,190,200];
  
  const updatedPrices=scores.map((price) => {
    return price + 20;
  });
  console.log(updatedPrices)
  
  20ずつ増えた配列が表示される

filter

const numbers = [1,4,5,8,10];


  const evenNumbers = numbers.filter((number)=>{
    if(number % 2 === 0){
      return true;
    }else{
      return false;
    }

  });
  console.log(evenNumbers);
}
偶数のみ別の配列で抽出される


↓ 省略

const evenNumbers = numbers.filter(number => % 2 === 0);

console.log(evenNumbers);


object

{
  const point ={
    x: 100,
    y: 180,
  };

  const keys = Object.keys(point);
  keys.forEach(key =>{
    console.log(`Key: ${key} Value: ${point[key]}`);
  });
  
  Key:x Value: 100
  Key:y Value: 180
  が表示される

  const points =[
    {x: 30, y:20},
    {x: 10, y:50},
    {x: 40, y:40},
  ];

  console.log(points[1].y);
  50が表示される

}

変数の挙動

  let x = [1,2];
  let y = x;
  x[0] = 5;
  console.log(x);//[5,2]
  console.log(y);//[5,2]
  場所が保存されるので同じになる
  
  let x = [1,2];
  let y = [...x];
  x[0] = 5;
  console.log(x);//[5,2]
  console.log(y);//[1,2]
  スプレッド演算子を使うとxの値が展開され値そのものが代入される

文字列

 const str = 'hello';

 console.log(str.length);
 5

 console.log(str.substring(2,4));
 ll

 console.log(str[1]);
 e

join(配列の要素を文字列として結合)

  const d = [2019,11,14];

  console.log(d.join('/'));
  2019/11/14と表示される

split(文字列を分割し配列にする)

  const t = '17:08:24';
  console.log(t.split(':'));
  ["17","08","24"]となる

  const [hour,minute,second] = t.split(':');
  console.log(hour);
  17
  console.log(minute);
  08
  console.log(second);
  24
0
0
1

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?