admin管理员组

文章数量:1313347

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?

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?

Share Improve this question edited Jul 31, 2018 at 19:23 Davi Resio asked Jul 31, 2018 at 19:12 Davi ResioDavi Resio 6933 gold badges11 silver badges22 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5

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}]

Have you already tried iterating over the array and checking the value of the key property each time you iterate?

Lodash's _.forEach() method is very good for this. You can iterate over an object:

_.forEach(someObject, function(value,key) { /* some function */})

or you can iterate over an array:

_.forEach(someArray, function(value) { if (value.key == 'someVal') {/*code*/}})

Then inside the forEach loop you can check your values and if they match, push to your other array.

本文标签: javascripthow filter object in json array by one keyStack Overflow