回答編集履歴

1

情報の追加。

2016/07/28 19:08

投稿

kei344
kei344

スコア69407

test CHANGED
@@ -43,3 +43,53 @@
43
43
  console.log( seek( datas ) );
44
44
 
45
45
  ```**動くサンプル:**[https://jsfiddle.net/12h5j2rm/](https://jsfiddle.net/12h5j2rm/)
46
+
47
+
48
+
49
+ ---
50
+
51
+
52
+
53
+ **追記:**
54
+
55
+
56
+
57
+ 再起しないやつもついでに。
58
+
59
+
60
+
61
+ ```JavaScript
62
+
63
+ var datas=[{name:'html',children:[{name:'head',children:[{name:'meta',children:[-3,-2,-1]},{name:'title',children:[0,1,2]}]},{name:'head',children:[{name:'meta',children:[3,4,5]},{name:'title',children:[6,7,8]}]}]}];
64
+
65
+ function seekWhile( ar ) {
66
+
67
+ var tm = [], crr;
68
+
69
+ while( ar.length ) {
70
+
71
+ crr = ar.shift();
72
+
73
+ if ( crr.hasOwnProperty( 'name' ) && crr.hasOwnProperty( 'children' ) ) {
74
+
75
+ if ( crr.name === 'title' ) {
76
+
77
+ tm.push( crr.children );
78
+
79
+ } else {
80
+
81
+ Array.prototype.push.apply( ar, crr.children );
82
+
83
+ }
84
+
85
+ }
86
+
87
+ }
88
+
89
+ return tm;
90
+
91
+ }
92
+
93
+ console.log( 'seekWhile:', seekWhile( datas ) );
94
+
95
+ ```**動くサンプル:**[https://jsfiddle.net/12h5j2rm/1/](https://jsfiddle.net/12h5j2rm/1/)