LoginSignup
0
0

More than 1 year has passed since last update.

オブジェクトのデータの持ち方について

Last updated at Posted at 2021-09-16

javascriptのオブジェクトについて勉強した。(jquery)
工夫したのはデータの持ち方(hobbyオブジェクト)について。

hobbyのbookやらsportsやらをたくさん外部から設定できる。

[追記]2021.09.17
以下のソースが冗長だったので、頂いたコメントを参考に
jquery的に書き直しました。

objectuse.js
let a = {};
a = user.getbookhobby();
$.each(a, function(key, val){
    console.log(val);
});
objectuse.js
$(function(){
let user = {
    name: '山田',
    age: 27,
    address: '東京',
    hobby:{
        'book':{},
        'sports':{},
    },
    getbookhobby:function(){
        return user.hobby.book;
    },
    setbookhobby:function($key, $value){
      user.hobby.book[$key] = $value;
    },
    gethobby:function(){
        return user.hobby;
    },
};
user.setbookhobby('0', 'AAA');
user.setbookhobby('1', 'BBB');
user.setbookhobby('2', 'CCC');

/*
let a = {};
a = user.getbookhobby();
$.each(a, function(key, val){
    console.log(val);
});
*/
$.each(user.getbookhobby(), (key, val) => {
    console.log(val);
});

});
0
0
2

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