Get Object Value From String Path | JavaScript Interview Question - 34 | Learnersbucket

preview_player
Показать описание
JavaScript Interview Question - 34 | In this video, we will see how to solve a medium-difficulty problem asked in frontend engineer interview to SDE1, SDE2, and SDE3.

We will see how to fetch a value from the string path in an object. This problem is the polyfill for _.get() lodash method.

Loved the question? I have 120+ such solved problems in my ebook "JavaScript Interview Guide". If you are preparing for Interviews and looking for a solutions book, Get my Ebook.

Social links
Рекомендации по теме
Комментарии
Автор

I am waiting for all your videos.
Since, it's been long time. I haven't seen any videos from you!
Please, continue videos. Thanks!

madhuiitb-cse
Автор

The given solution only work for the single character keys. So I extended the solution to more generic one:


const obj = {
abc: {
b: {
c: [1, 2, 3]
}
}
}

function get(obj, stringPath) {
const converyStringPathToArray = (stringPath) => {
const keys = []
const excludeChars = ['.', '[', ']']
let currKey = "";
for(let i = 0; i < stringPath.length; i++) {
{
currKey += stringPath[i]
} else {
if(currKey.length !== 0) {
keys.push(currKey)
currKey = ""
}
}
}
if(currKey.length !== 0) {
keys.push(currKey)
}
return keys;
}

const keys = Array.isArray(stringPath) ? stringPath:
return keys.reduce((obj, key) => obj[key], obj);
}

console.log(get(obj, 'abc.b.c'))
console.log(get(obj, 'abc.b.c.0'))
console.log(get(obj, 'abc.b.c[1]'))
console.log(get(obj, ['abc', 'b', 'c', '2']))
console.log(get(obj, 'abc.b.c[3]'))
console.log(get(obj, 'abc.c'))
console.log(get(obj, 'abc[b[c[1]]]'))

Output:


(3) [1, 2, 3]
1
2
3
undefined
undefined
2

ShankarKumar-dgpe
Автор

Saw your post #100daysToCrackJavaScript on Linkedin. Can we have a separate playlist for it? Planning to follow it. Thanks!

puneetsingh
Автор

Hi Prashant, solved it on my own.

//Below is my code
const get = (obj, path) => {
let keyArr = path

if(typeof path === 'string'){
path = path.replace('[', '.')
path = path.replace(']', '')
keyArr = path.split('.')
keyArr.reverse()
}

const helper = (obj, keyArr) => {
if(keyArr.length === 0){
return obj
}
let nextKey = keyArr.pop()
if(Array.isArray(obj)){
return helper(obj[parseInt(nextKey)], keyArr)
}
return helper(obj[nextKey], keyArr)
}

return helper(obj, keyArr)
}


const obj = {
a: {
b: {
c: [1, 2, 3]
}
}
};

console.log(get(obj, 'a.b.c'));
console.log(get(obj, 'a.b.c.0'));
console.log(get(obj, 'a.b.c[1]'));
console.log(get(obj, 'a.b.c[3]'));
console.log(get(obj, 'a.b'));

akash-kumar
Автор

So this is not for frontend interviews?

amazingtechnology
Автор

Is it a good approach ?

const get = (obj, str) => {
const helper = (obj, str) => {
if (str.length <= 0) return obj;
if (obj[str[0]] === undefined) return undefined;
return helper(obj[str[0]], str.slice(1));
};

if (typeof str === 'string') {
str = str.replace( '[', "." ).replace( ']', "" ).split('.');
}

return helper(obj, str);
};

sudhanshuranjan
Автор

Where can we get more such questions for practice? Is there any website apart from yours?

chahatbhatia
Автор

const obj={
a:{
b:{
c:"hii" }
}
}
function get(obj, hlo) {
let objti={}
let peak=""
for(key in obj){
const val = obj[key];
const newkey=peak==""?key:peak +"."+key;
if(val!==null && typeof val==="object"){
let recurive= get(val, newkey);
objti={...objti, ...recurive}
}
else{
objti[newkey]=val;
}

}
for(var key in objti){
if(objti[key]==hlo){
return objti[key];
}
else{
console.log(hlo)
}
}

}
console.log(get(obj, "a.b.c"))
Pls review this is it correct

jatin
welcome to shbcf.ru