Finding value of deeply nested objects with object keys.
function findKeyValue ( paymentObj , key ) {
const keySplit = key . split ( '.' ) ;
let value = '' ;
function findValue ( obj , first , ...keys ) {
if ( typeof obj [ first ] === 'object' ) {
if ( keys ) {
findValue ( obj [ first ] , ...keys ) ;
}
} else {
value = obj [ first ] ;
}
}
findValue ( paymentObj , ...keySplit ) ;
return value ;
}
Finding the first occurence in a string
const input = "ABCA" ;
function firstOccur ( input ) {
let countHash = { } ;
let inputAsArray = input . split ( '' ) ;
for ( var i in inputAsArray ) {
let key = inputAsArray [ i ] ;
if ( Object . keys ( countHash ) . includes ( key ) ) {
return count [ key ] ;
} else {
countHash [ inputAsArray [ i ] ] = 1 ;
}
}
return 'none' ;
}
const output = firstOccur ( input ) ;
Finding number of occurences for a character in a given string
const input = "ABCA" ;
function firstOccur ( input ) {
let countHash = { } ;
let inputAsArray = input . split ( '' ) ;
for ( var i in inputAsArray ) {
let key = inputAsArray [ i ] ;
if ( Object . keys ( countHash ) . includes ( key ) ) {
countHash [ key ] = countHash [ key ] + 1 ;
} else {
countHash [ inputAsArray [ i ] ] = 1 ;
}
}
return countHash ;
}
const output = firstOccur ( input ) ;