This is my datas
const tabledata =
{ 0:
{ Id: 1
, name: 'test1'
, parameters:
[ { name: 'application_instance', value: 'home', type: 'generic' }
, { name: 'application_name', value: 'initial', type: 'generic' }
, { name: 'application_role', value: 'disk_init_role', type: 'generic' }
, { name: 'customer_environment', value: 'development', type: 'generic' }
, { name: 'customer_name', value: 'guest', type: 'generic' }
] }
, 1:
{ Id: 2
, name: 'test2'
, parameters:
[ { name: 'application_instance', value: 'home', type: 'generic' }
, { name: 'application_name', value: 'disk_init', type: 'generic' }
, { name: 'application_role', value: 'initial', type: 'generic' }
, { name: 'customer_environment', value: 'development', type: 'generic' }
, { name: 'customer_name', value: 'guest', type: 'generic' }
] }
, 2:
{ Id: 3
, name: 'test3'
, parameters:
[ { name: 'application_instance', value: 'home', type: 'generic' }
, { name: 'application_name', value: 'initial', type: 'generic' }
, { name: 'application_role', value: 'initial', type: 'generic' }
, { name: 'customer_environment', value: 'development', type: 'generic' }
, { name: 'customer_name', value: 'guest', type: 'generic' }
] }
, 3:
{ Id: 4
, name: 'test4'
, parameters:
[ { name: 'application_instance', value: 'home', type: 'generic' }
, { name: 'application_name', value: 'initial', type: 'generic' }
, { name: 'application_role', value: 'disk_init_role', type: 'generic' }
, { name: 'customer_environment', value: 'development', type: 'generic' }
, { name: 'customer_name', value: 'guest', type: 'generic' }
] }
, 5:
{ Id: 5
, name: 'test5'
, parameters:
[ { name: 'application_instance', value: 'home', type: 'generic' }
, { name: 'application_name', value: 'initial_disk_init', type: 'generic' }
, { name: 'application_role', value: 'disk_initial', type: 'generic' }
, { name: 'customer_environment', value: 'development', type: 'generic' }
, { name: 'customer_name', value: 'guest', type: 'generic' }
] } }
I need to filter all data that has name
of application_role
and a value
of initial
I tried different filters but they al give the wrong outcome because in the application_name
there is also a value of initial and the filter add this to the outcome.
So the data need to be explicit to:
parameters[x].name === 'application_role'
parameters[x].value === 'initial'
Use Array.prototype.filter
and Array.prototype.some
for achieving your goal. Try this-
var data={0:{Id:1,name:'test1',parameters:[{name:'application_instance',value:'home',type:'generic'},{name:'application_name',value:'initial',type:'generic'},{name:'application_role',value:'disk_init_role',type:'generic'},{name:'customer_environment',value:'development',type:'generic'},{name:'customer_name',value:'guest',type:'generic'}]},1:{Id:2,name:'test2',parameters:[{name:'application_instance',value:'home',type:'generic'},{name:'application_name',value:'disk_init',type:'generic'},{name:'application_role',value:'initial',type:'generic'},{name:'customer_environment',value:'development',type:'generic'},{name:'customer_name',value:'guest',type:'generic'}]},2:{Id:3,name:'test3',parameters:[{name:'application_instance',value:'home',type:'generic'},{name:'application_name',value:'initial',type:'generic'},{name:'application_role',value:'initial',type:'generic'},{name:'customer_environment',value:'development',type:'generic'},{name:'customer_name',value:'guest',type:'generic'}]},3:{Id:4,name:'test4',parameters:[{name:'application_instance',value:'home',type:'generic'},{name:'application_name',value:'initial',type:'generic'},{name:'application_role',value:'disk_init_role',type:'generic'},{name:'customer_environment',value:'development',type:'generic'},{name:'customer_name',value:'guest',type:'generic'}]},5:{Id:5,name:'test5',parameters:[{name:'application_instance',value:'home',type:'generic'},{name:'application_name',value:'initial_disk_init',type:'generic'},{name:'application_role',value:'disk_initial',type:'generic'},{name:'customer_environment',value:'development',type:'generic'},{name:'customer_name',value:'guest',type:'generic'}]}};
const res = Object.values(data).filter(
item => item.parameters.some(param => param.name === 'application_role' && param.value === 'initial')
);
console.log({...res});
.as-console-wrapper {min-height: 100%!important; top: 0}