Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how filter object in json array by one key

i making a system with angular 6 and i have more 100 inputs for make, but i have a lot of forms, and the forms using the some input much times. beucause it i want create dynamic form. but i'm think how the best form to connect the configure of input to correct object.

im thinking create all definition of inputs and catch this values based object, but my definition was do it

 let inputs: FormBase<any>[] = [

      new InputTextForm({
        key: 'Id',
        label: 'Id',
        value: '',
        required: true,
        order: 1
      }),

      new InputTextForm({
        key: 'Nome',
        label: 'Nome',
        value: '',
        required: true,
        order: 2
      }),

      new InputTextForm({
        key: 'Descricao',
        label: 'Descricao',
        order: 3
      }),

      new InputTextForm({
        key: 'PodeFracionar',
        label: 'Pode Fracionar',
        order: 4
      }),

      new InputTextForm({
        key: 'TaxaPis',
        label: 'Taxa Pis',
        order: 5
      }),
    ];


    return inputs.sort((a, b) => a.order - b.order);
  }

but name of all objects is InputTextForm, and i will need filter based on key. but how i can filter and push one InputTextForm to other array filtering by key?

like image 757
Davi Resio Avatar asked Sep 12 '26 08:09

Davi Resio


1 Answers

You don't have to use lodash here array object have filter method will do the same , you may consider use native javascript function then inject a new dependency to your project that will give same result

let result = inputs.filter(i => i.key === 'TaxaPis') ; 
console.log(result);  // => [{key: 'TaxaPis', label: 'Taxa Pis',  order: 5}]
like image 155
Muhammed Albarmavi Avatar answered Sep 13 '26 21:09

Muhammed Albarmavi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!