Socket
Book a DemoInstallSign in
Socket

@ngha/nested-value

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ngha/nested-value

nestedValue the value at `path` of `object`

latest
npmnpm
Version
0.2.5
Version published
Weekly downloads
8
33.33%
Maintainers
1
Weekly downloads
 
Created
Source

nested-value

This library get object value by path

Usage

Import NestedValueModule to your module

import { NgModule } from '@angular/core';
import { BrowserModule  } from '@angular/platform-browser';
import { AppComponent } from './app';
 
import { NestedValueModule } from '@ngha/nested-value';
@NgModule({
  imports: [NestedValueModule],
})
export class AppModule {}

nested-value

Returns value of an object value

Usage: object | nestedValue: 'path' : 'defaultValue'

  <p>{{{ 'a': 'aa' } | nestedValue:'a'}}</p>
  <!-- Output: "aa" -->
  <p>{{{ c: [{ d: '123', e: 'abc' }, { d: '456', e: 'def' }] } | nestedValue:'c[0].d'}}</p>
  <!-- Output: "123" -->
  <p>{{{ c: [{ d: '123', e: 'abc' }, { d: '456', e: 'def' }] } | nestedValue:['c', [1], 'e']}}</p>
  <!-- Output: "def" -->
  <p>{{{ c: [{ d: '123', e: 'abc' }, { d: '456', e: 'def' }] } | nestedValue:['c', [3], 'f']: 'default'}}</p>
  <!-- Output: "default" -->
  <p>{{{ c: [{ d: '123', e: 'abc' }, { d: '456', e: 'def' }] } | nestedValue:['c', [3], 'f']}}</p>
  <!-- Output: undefine -->
 import {NestedValueService} from '@ngha/nested-value'
 
 @Component({
   providers: [TransformObjectService],
 })
 export class AppComponent
 {
   constructor(private service: TransformObjectService)
   {
     const obj = {
      aa: [{ b: { c: 0 }, 1: 0 }],
      dd: { ee: { ff: 2 } },
      gg: { h: 2 },
      'gg.h': 1,
      'kk.ll': { 'mm.n': [3, 4, { 'oo.p': 5 }, { 'oo': { p: 6 } }] },
      tt: null
     };

      const result1 = this.service.nestedValue(obj , 'aa[0].b.c' ) === obj['aa'][0]['b']['c'] 
      // Output: result1 = true
      const result2 = this.service.nestedValue(obj , 'aa[0][1]') === obj['aa'][0][1]
      // Output: result2 = true
      const result3 = this.service.nestedValue(obj , ['kk.ll', 'mm.n', 2, 'oo.p']) === obj['kk.ll']['mm.n'][2]['oo.p']
      // Output: result3 = true
      const result4 = this.service.nestedValue(obj , ['kk.ll', 'mm.n', 3, 'oo', 'p']) === 6
      // Output: result4 = true
      const result5 = this.service.nestedValue(undefine , ['kk'] , 1) === 1
      // Output: result5 = true
      const result6 = this.service.nestedValue(undefine , 'kk') === undefine
      // Output: result6 = true

   }
 
 }

Keywords

Angular

FAQs

Package last updated on 20 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts