JSFiddle - React, Tailwind, and code Playground
by Alexander
JavaScript
console.clear();
const data = {
user: {
posts: [
{ title: 'Foo', comments: [ 'abc', 'def' ] },
{ title: 'Bar', comments: ['Ok'] },
{ title: 'Buz', comments: null, description: 'Some data' },
]
}
}
const get = (...data) => obj => (
(data[0].raw != void 0 && (data[0] = data[0].toString())),
get.$data$ = (
(
(data.length == 1 && typeof data[0] == 'string')
||
(data.length == 2 && typeof data[0] == 'string' && 'string' == data[1])
)
? data[0].split(/[\.\/\s\W]+/g)
: data
),
get.$data$.reduce(
(a, b) => (
(
( void 1 != a && void 0 != a[b] )
? a[b]
: (
data[1] != void 0
? data[1]
: void 0
)
),
obj
)
)
);
console.log([data].map(get `user.posts.1.comments`)[0]);
console.log( get `user.posts.1/comments` (data) );
/*
console.log( get('user', 'posts', 1, 'comments').from(data) );
console.log( get('user', 'post', 2, 'comments').from(data) );
console.log( get('user/posts/1/comments').from(data) );
console.log( get('user.posts.1.comments').from(data) );
console.log( get('user posts 1 comments').from(data) );
console.log( get('user > posts > 1 > comments').from(data) );
console.log( get `user:posts:1:comments:foo` .from(data, 'foo') );
//console.log( get({"user":{"posts":{"1" : "comments" }}}).from(data) );
//*/