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 02 Answers
Reset to default 5You 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
版权声明:本文标题:javascript - how filter object in json array by one key - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741946140a2406425.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论