0
0

More than 1 year has passed since last update.

【JavaScript】【Jquery】配列操作 map文 eachよりも優れてる?

Last updated at Posted at 2023-07-26

//varを使用すると再宣言、初期化によるバグを起こしやすくなるため、今は非推奨だから使わないほうが良い

let results = [
    {'name' : '田中',  'japanese' : 83, 'math' : 57, 'science' : 43,  'social' : 72, 'english' : 78},
    {'name' : '渡部',  'japanese' : 62, 'math' : 88, 'science' : 70,  'social' : 66, 'english' : 38},
    {'name' : '品川',  'japanese' : 23, 'math' : 33, 'science' : 53,  'social' : 17, 'english' : 07},
    {'name' : '滝谷',  'japanese' : 93, 'math' : 89, 'science' : 79,  'social' : 96, 'english' : 91},
    {'name' : '川原田','japanese' : 55, 'math' : 56, 'science' : 60,  'social' : 54, 'english' : 66},
    {'name' : '一色',  'japanese' : 27, 'math' : 47, 'science' : 100, 'social' : 42, 'english' : 38},
];

let result = $.map(results, function(e) {
  return e.name;
});

console.log(result);

let array = [100, 110, 120, 130, 140, 150];
let new_array = $.map(array, function(num) {
  return num + 10;
});

console.log(new_array);

結果

[ "田中" , "渡部" , "品川" , "滝谷" , "川原田" , "一色" ]

[110, 120, 130, 140, 150, 160]

DOM操作の場合

<input type="checkbox" name="sports" value="野球">野球
<input type="checkbox" name="sports" value="サッカー">サッカー
<input type="checkbox" name="sports" value="バスケットボール">バスケットボール
let sports = $('input[type="checkbox"]').map(function(){
    return this.value;
}).get();

let sports = $('input[type="checkbox"]').map((_,e) => e.value).get();

console.log(sports);

結果

["野球", "サッカー", "バスケットボール"]
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