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

按另一个对象引用数组对一组对象进行排序

  •  4
  • sonEtLumiere  · 技术社区  · 3 年前

    我需要一些帮助来整理这些数据,我有一系列的产品,我需要排序和显示设置配置。输出的顺序必须与设置数组(索引)的顺序相同,如果显示为真。提前谢谢。这就是我尝试的:

    var products = [
        {id: 0, name: 'Chocolate', category: 'Sweet'},
        {id: 1, name: 'Almendras', category: 'Fruit'},
        {id: 2, name: 'Nueces', category: 'Fruit'},
        {id: 3, name: 'Mermelada', category: 'Jam'},
        {id: 4, name: 'Alfajor', category: 'Sweet'},
        {id: 5, name: 'Queso', category: 'UwU'},
        {id: 6, name: 'Arandanos', category: 'Fruit'},
        {id: 7, name: 'Maracuya', category: 'Fruit'}
    ];
    
    let settings = [
        {
            name: 'Fruit',
            display: true
        },
        {
            name: 'Jam',
            display: false
        },
        {
            name: 'Sweet',
            display: true
        },
        {
            name: 'UwU',
            display: true
        }
    ]
    
    
    let group = products.reduce((r, a) => {
          r[a.category] = [...r[a.category] || [], a];
          return r;
        }, {});
    
    let arrangedProducts = Object.keys(group); 
    
    console.log(group);
    console.log(arrangedProducts);

    /*
    expected result = [
        [
            {id: 1, name: 'Almendras', category: 'Fruit'}, 
            {id: 2, name: 'Nueces', category: 'Fruit'}, 
            {id: 6, name: 'Arandanos', category: 'Fruit'},
            {id: 7, name: 'Maracuya', category: 'Fruit'}
        ],
        [
            {id: 0, name: 'Chocolate', category: 'Sweet'},
            {id: 4, name: 'Alfajor', category: 'Sweet'}
        ],
        [
            {id: 5, name: 'Queso', category: 'UwU'}
        ]
    ]
    */
    3 回复  |  直到 3 年前
        1
  •  1
  •   Nick    3 年前

    你可以 filter settings display 属性,然后使用 Array.map 返回中的对象列表的步骤 products 与类别相匹配的:

    const products = [
        {id: 0, name: 'Chocolate', category: 'Sweet'},
        {id: 1, name: 'Almendras', category: 'Fruit'},
        {id: 2, name: 'Nueces', category: 'Fruit'},
        {id: 3, name: 'Mermelada', category: 'Jam'},
        {id: 4, name: 'Alfajor', category: 'Sweet'},
        {id: 5, name: 'Queso', category: 'UwU'},
        {id: 6, name: 'Arandanos', category: 'Fruit'},
        {id: 7, name: 'Maracuya', category: 'Fruit'}
    ];
    
    const settings = [
        { name: 'Fruit', display: true },
        { name: 'Jam', display: false },
        { name: 'Sweet', display: true },
        { name: 'UwU', display: true }
    ];
    
    const result = settings
      .filter(c => c.display)
      .map(c => products.filter(o => o.category == c.name));
    
    console.log(result);

    产品 设置 display:true ,因此,对于大型阵列,速度可能较慢。然而 filter 它的开销非常低,使用OP的示例数据进行测试表明,它的运行速度是 reduce 版本还有一个更大的 产品 阵列(99个条目)运行速度提高10倍。

        2
  •  4
  •   MUHAMMAD ILYAS    3 年前

    1. 分组
    2. 应用设置并检索结果

    const products = [
      { id: 0, name: "Chocolate", category: "Sweet" },
      { id: 1, name: "Almendras", category: "Fruit" },
      { id: 2, name: "Nueces", category: "Fruit" },
      { id: 3, name: "Mermelada", category: "Jam" },
      { id: 4, name: "Alfajor", category: "Sweet" },
      { id: 5, name: "Queso", category: "UwU" },
      { id: 6, name: "Arandanos", category: "Fruit" },
      { id: 7, name: "Maracuya", category: "Fruit" },
    ];
    
    const productsGroup = products.reduce((r, a) => {
      r[a.category] = [...(r[a.category] || []), a];
      return r;
    }, {});
    
    function applySettings(settings) {
      return settings.filter((s) => s.display).map((s) => productsGroup[s.name]);
    }
    
    console.log(
      applySettings([
        {
          name: "Fruit",
          display: true,
        },
        {
          name: "Jam",
          display: false,
        },
      ])
    );
    
    console.log(
      applySettings([
        {
          name: "Fruit",
          display: true,
        },
        {
          name: "Sweet",
          display: true,
        },
        {
          name: "UwU",
          display: true,
        },
      ])
    );
        3
  •  1
  •   StackSlave    3 年前

    continue display false :

    var products = [
        {id: 0, name: 'Chocolate', category: 'Sweet'},
        {id: 1, name: 'Almendras', category: 'Fruit'},
        {id: 2, name: 'Nueces', category: 'Fruit'},
        {id: 3, name: 'Mermelada', category: 'Jam'},
        {id: 4, name: 'Alfajor', category: 'Sweet'},
        {id: 5, name: 'Queso', category: 'UwU'},
        {id: 6, name: 'Arandanos', category: 'Fruit'},
        {id: 7, name: 'Maracuya', category: 'Fruit'}
    ];
    
    let settings = [
        {
            name: 'Fruit',
            display: true
        },
        {
            name: 'Jam',
            display: false
        },
        {
            name: 'Sweet',
            display: true
        },
        {
            name: 'UwU',
            display: true
        }
    ];
    function sortProducts(){
      const r = [];
      let i = -1;
      for(let s of settings){
        if(!s.display){
          continue;
        }
        i++;
        for(let o of products){
          if(s.name === o.category){
            if(r[i]){
              r[i].push(o);
            }
            else{
              r.push([o]);
            }
          }
        }
      }
      return r;
    }
    console.log(sortProducts());