Recursively check object for first array
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
I am recursively checking an object to:
- Find the largest JSON object
- Find the array within it
It has to be rather flexible, as it should work with any structure of JSON data.
The below works, but it isn't flexible for all structures of JSON.
const data = foo: 1, bar: magic: , baz: woo: some: test: ['result']
const getField = (fields, item) =>
if (fields.length < 1)
return item
else
return fields.split('.').reduce((a, b) => a[b], item)
const checkObj = obj => Object.entries(obj).map(
([key, value]) =>
return len: JSON.stringify(value).length, key: key, arr: Array.isArray(value)
)
const findArr = obj =>
let checked = ''
let itm = checkObj(getField(checked, data))
while (itm.some(e => !e.arr))
itm = checkObj(getField(checked, data))
const longestObj = itm.map(e => e).sort((a, b) => b.len - a.len)[0].key
// Find longest key
checked = checked.length > 1 ? `$checked.$longestObj` : longestObj
// Track keys checked
return checked
;
Test it out:
https://codesandbox.io/s/lr0ry18j2l
javascript array json
add a comment |Â
up vote
0
down vote
favorite
I am recursively checking an object to:
- Find the largest JSON object
- Find the array within it
It has to be rather flexible, as it should work with any structure of JSON data.
The below works, but it isn't flexible for all structures of JSON.
const data = foo: 1, bar: magic: , baz: woo: some: test: ['result']
const getField = (fields, item) =>
if (fields.length < 1)
return item
else
return fields.split('.').reduce((a, b) => a[b], item)
const checkObj = obj => Object.entries(obj).map(
([key, value]) =>
return len: JSON.stringify(value).length, key: key, arr: Array.isArray(value)
)
const findArr = obj =>
let checked = ''
let itm = checkObj(getField(checked, data))
while (itm.some(e => !e.arr))
itm = checkObj(getField(checked, data))
const longestObj = itm.map(e => e).sort((a, b) => b.len - a.len)[0].key
// Find longest key
checked = checked.length > 1 ? `$checked.$longestObj` : longestObj
// Track keys checked
return checked
;
Test it out:
https://codesandbox.io/s/lr0ry18j2l
javascript array json
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am recursively checking an object to:
- Find the largest JSON object
- Find the array within it
It has to be rather flexible, as it should work with any structure of JSON data.
The below works, but it isn't flexible for all structures of JSON.
const data = foo: 1, bar: magic: , baz: woo: some: test: ['result']
const getField = (fields, item) =>
if (fields.length < 1)
return item
else
return fields.split('.').reduce((a, b) => a[b], item)
const checkObj = obj => Object.entries(obj).map(
([key, value]) =>
return len: JSON.stringify(value).length, key: key, arr: Array.isArray(value)
)
const findArr = obj =>
let checked = ''
let itm = checkObj(getField(checked, data))
while (itm.some(e => !e.arr))
itm = checkObj(getField(checked, data))
const longestObj = itm.map(e => e).sort((a, b) => b.len - a.len)[0].key
// Find longest key
checked = checked.length > 1 ? `$checked.$longestObj` : longestObj
// Track keys checked
return checked
;
Test it out:
https://codesandbox.io/s/lr0ry18j2l
javascript array json
I am recursively checking an object to:
- Find the largest JSON object
- Find the array within it
It has to be rather flexible, as it should work with any structure of JSON data.
The below works, but it isn't flexible for all structures of JSON.
const data = foo: 1, bar: magic: , baz: woo: some: test: ['result']
const getField = (fields, item) =>
if (fields.length < 1)
return item
else
return fields.split('.').reduce((a, b) => a[b], item)
const checkObj = obj => Object.entries(obj).map(
([key, value]) =>
return len: JSON.stringify(value).length, key: key, arr: Array.isArray(value)
)
const findArr = obj =>
let checked = ''
let itm = checkObj(getField(checked, data))
while (itm.some(e => !e.arr))
itm = checkObj(getField(checked, data))
const longestObj = itm.map(e => e).sort((a, b) => b.len - a.len)[0].key
// Find longest key
checked = checked.length > 1 ? `$checked.$longestObj` : longestObj
// Track keys checked
return checked
;
Test it out:
https://codesandbox.io/s/lr0ry18j2l
javascript array json
edited Apr 17 at 13:15
asked Apr 17 at 12:45
Ycon
1467
1467
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192286%2frecursively-check-object-for-first-array%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password