数组中的javascript筛选器数据数组

fxnxkyjh  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(388)

这是我的档案

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

我需要过滤所有已删除的数据 name 属于 'application_role'value 属于 'initial' 我尝试了不同的过滤器,但它们都给出了错误的结果,因为在应用程序的名称中有一个olso值initial,过滤器将此值添加到结果中。
因此,数据需要明确说明:
parameters[x].name === 'application_role' parameters[x].value === 'initial'

ewm0tg9j

ewm0tg9j1#

使用 Array.prototype.filterArray.prototype.some 为了实现你的目标。试试这个-

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}

相关问题