-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest23.js
More file actions
39 lines (36 loc) · 743 Bytes
/
test23.js
File metadata and controls
39 lines (36 loc) · 743 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
collectStrings
Write a function called collectStrings which accepts an object and returns
an array of all the values in the object that have a typeof string
*/
const obj = {
stuff: 'foo',
data: {
val: {
thing: {
info: 'bar',
moreInfo: {
evenMoreInfo: {
weMadeIt: 'baz'
}
}
}
}
}
};
const collectStrings = obj => {
let arr = [];
const help = obj => {
for (let k in obj) {
if (typeof obj[k] === 'string') {
arr.push(obj[k]);
} else if (typeof obj[k] === 'object') {
// console.log(obj[k]);
help(obj[k]);
}
}
};
help(obj);
return arr;
};
console.log(collectStrings(obj)); // ["foo", "bar", "baz"])