Created
June 27, 2014 06:43
-
-
Save south-str/9040effb64cf336a9d61 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.onload = function(){ | |
//ネストしたObjectから望みの値を取り出す | |
var a = { | |
"a":{ | |
"aa":0, | |
"ab":1, | |
"ac":2, | |
"ad":3}, | |
"b":{ | |
"aa":10, | |
"ab":11, | |
"ac":12, | |
"ad":13}, | |
"c":{ | |
"aa":20, | |
"ab":21, | |
"ac":22, | |
"ad":23}, | |
"d":{ | |
"aa":30, | |
"ab":31, | |
"ac":32, | |
"ad":33} | |
}; | |
console.log(maps(map(a), "ad").reduce(function(x, y){return x + y;})); | |
console.log(allProperty(a)); | |
console.log(m(a)); | |
} | |
/* 指定したkeyの値を配列に格納する。 | |
* @input o Object | |
* @input s String | |
* @return a Array | |
*/ | |
function maps(o, s){ | |
var a = []; | |
for(key in o){ | |
for(k in o[key]){ | |
if(k == s){ | |
a.push(o[key][k]); | |
} | |
} | |
} | |
return a; | |
} | |
/* オブジェクトの中身を配列に格納する。 | |
* @input o Object | |
* @return a Array | |
*/ | |
function map(o){ | |
var a = []; | |
for(key in o){ | |
a.push(o[key]); | |
} | |
return a; | |
} | |
/* mapを書き直したもの | |
* @input o Object | |
* @return a Array | |
*/ | |
function m(o){ | |
var a = []; | |
Object.keys(o).forEach(function(x/*, y, z*/){ | |
a.push(o[x]); | |
}); | |
return a; | |
} | |
/* オブジェクトの要素を配列にして出力する。 | |
* @input o Object | |
* @return a Array | |
*/ | |
function allProperty(o){ | |
var a = []; | |
Object.keys(o).forEach(function(x/*, y, z*/){ | |
for(key in o[x]){ | |
a.push(o[x][key]); | |
} | |
}); | |
return a; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment