代码之家  ›  专栏  ›  技术社区  ›  developer

从多维对象数组中查找id

  •  1
  • developer  · 技术社区  · 6 年前

    我有一个多维对象数组:

    var products = [  
    { id: 1, groups: [ { id: 1.1, selected: true }, { id: 1.2, selected: false }}, 
    { id: 2, groups: [ { id: 2.1, selected: false }, { id: 2.2, selected: true }} ];
    

    如何使用ES6在一维数组中查找选定组(选定标志设置为true的组)的列表。

    预期结果:

    [1.1, 2.2]
    
    6 回复  |  直到 6 年前
        1
  •  0
  •   Pranay Rana    6 年前

    你可以利用 map , filter concat 函数并得到结果

    var products = [  
    { id: 1, groups: [ { id: 1.1, selected: true }, { id: 1.3, selected: true },{ id: 1.2, selected: false }]}, 
    { id: 2, groups: [ { id: 2.1, selected: false }, { id: 2.2, selected: true }]} ];
    
    const selectgroups =  [].concat(...products.map(x => x.groups.filter(g=> g.selected)));
    const selectedGroupIds = selectgroups.map(g=> g.id);
    
    console.log(selectedGroupIds);
    

    提供所有选定组列表的代码

    Working Demo

        2
  •  2
  •   Nenad Vracar    6 年前

    你可以用 reduce 方法和内部 forEach 循环以获取选定位置的组对象 true .

    var products = [  
    { id: 1, groups: [ { id: 1.1, selected: true }, { id: 1.2, selected: false }]}, 
    { id: 2, groups: [ { id: 2.1, selected: false }, { id: 2.2, selected: true }]} ];
    
    const result = products.reduce((r, {groups}) => {
      groups.forEach(e => e.selected && r.push(e.id));
      return r;
    }, [])
    	
    console.log(result)
        3
  •  2
  •   bugs    6 年前

    一个可能的替代方案。

    展开数组,然后提取所需信息。

    const products = [
        { id: 1, groups: [{ id: 1.1, selected: true }, { id: 1.2, selected: false }] },
        { id: 2, groups: [{ id: 2.1, selected: false }, { id: 2.2, selected: true }] }];
    
    const allIds = [].concat(...products.map(p => p.groups)).filter(g => g.selected).map(x => x.id)
    
    console.log(allIds)
        4
  •  1
  •   baao    6 年前

    您可以使用下面的reduce和filter,这将为您提供 selected: true . 注意,我将id:1.2更改为true,以演示获得多个true结果的能力。

    const products = [
      { id: 1, groups: [ { id: 1.1, selected: true }, { id: 1.2, selected: true } ]},
      { id: 2, groups: [ { id: 2.1, selected: false }, { id: 2.2, selected: true } ]} ];
    
    const res = products.reduce((a, {groups}) => 
      a.concat(...groups.filter(({selected}) => selected).map(({id}) => id))
    , []);
    
    console.log(res);
        5
  •  1
  •   Anshuman Jaiswal    6 年前

    var products = [  
    { id: 1, groups: [ { id: 1.1, selected: true }, { id: 1.2, selected: false }]}, 
    { id: 2, groups: [ { id: 2.1, selected: false }, { id: 2.2, selected: true } ]}];
    
    var desiredResult = products.map(product => {
      return product.groups.filter(group => group.selected).map(item => item.id).toString();
    });
    console.log(desiredResult);
        6
  •  0
  •   Matías Fidemraizer    6 年前

    我看到了一种完全不同的方法:使用专门的数据结构以您希望的方式对数据进行建模:

    // Store product id mapped to a set of
    // group ids
    const productGroupMap = new Map ( [
       [ 1, new Set ( [ 1.1, 1.2 ] ) ],
       [ 2, new Set ( [ 2.1, 2.2 ] ) ]
    ] )
    
    // Store group ids to their data
    const groupMap = new Map ( [
       [ 1.1, { title: "Title 1.1" } ],
       [ 1.2, { title: "Title 1.2" } ],
       [ 2.1, { title: "Title 2.1" } ],
       [ 2.2, { title: "Title 2.2" } ]
    ] )
    
    // Somewhere in your application, you would declare
    // a set of selected group ids, and you would add ids
    // when the user selects a given group
    const selectedGroups = new Set ()
    
    selectedGroups.add ( 1.1 )
    selectedGroups.add ( 2.2 )
    
    // Finally, when you want these selected groups, you just need to
    // map selected group ids to their group data getting it from the
    // groupMap
    const groups = [ ...selectedGroups ].map ( id => groupMap.get ( id ) )
    
    console.log ( groups )