代码之家  ›  专栏  ›  技术社区  ›  Rohìt Jíndal

为每个对象创建唯一的ID

  •  4
  • Rohìt Jíndal  · 技术社区  · 6 年前

    概述: 有一系列 product group 对象和具有数组的每个产品组 items . 产品组对象具有唯一ID,但项数组未分配任何唯一ID。请在json对象下面找到。

    const productList = [{
      productGroup : 'PG1',
      index: 1,
      items: [{
        item1: 'item1 value',
        item2: 'item2 value'
      },{
        item1: 'item1 value',
        item2: 'item2 value'
      },{
        item1: 'item1 value',
        item2: 'item2 value'
      }]
    }, {
      productGroup : 'PG2',
      index: 2,
      items: [{
        item1: 'item1 value',
        item2: 'item2 value'
      },{
        item1: 'item1 value',
        item2: 'item2 value'
      }]
    }];
    

    要求: 为的每个项对象分配一些唯一的ID 项目 基于产品组的数组。

    到目前为止我试过: working solution : concat index of product group with the index of each list items

    const productList = [{
      productGroup : 'PG1',
      index: 1,
      items: [{
        item1: 'item1 value',
        item2: 'item2 value'
      },{
        item1: 'item1 value',
        item2: 'item2 value'
      },{
        item1: 'item1 value',
        item2: 'item2 value'
      }]
    }, {
      productGroup : 'PG2',
      index: 2,
      items: [{
        item1: 'item1 value',
        item2: 'item2 value'
      },{
        item1: 'item1 value',
        item2: 'item2 value'
      }]
    }];
    
    const itemList = productList.map(obj => {
    	obj.items.map((itemObj, index) => {
      	itemObj.productGroup = obj.productGroup;
        itemObj.pgIndex = obj.index;
        itemObj.itemIndex = obj.index + '' + index;
      });
      return obj.items;
    });
    
    var mergedListItems = itemList.reduce((arr, arrNext) => arr.concat(arrNext));
    
    console.log('mergedListItems', mergedListItems);

    这种方法好吗?如果没有,那你能帮我找到最好的方法吗?

    4 回复  |  直到 6 年前
        1
  •  1
  •   CertainPerformance    6 年前

    你可能 .reduce 立即进入输出数组,而不是重复多次:

    const productList = [{
      productGroup : 'PG1',
      index: 1,
      items: [{
        item1: 'item1 value',
        item2: 'item2 value'
      },{
        item1: 'item1 value',
        item2: 'item2 value'
      },{
        item1: 'item1 value',
        item2: 'item2 value'
      }]
    }, {
      productGroup : 'PG2',
      index: 2,
      items: [{
        item1: 'item1 value',
        item2: 'item2 value'
      },{
        item1: 'item1 value',
        item2: 'item2 value'
      }]
    }];
    
    const mergedListItems = productList.reduce((a, { productGroup, index, items }) => {
      items.forEach((itemObj, itemIndex) => {
        a.push({
          ...itemObj,
          productGroup,
          pgIndex: index,
          itemIndex: `${index}${itemIndex}`
        });
      });
      return a;
    }, []);
    console.log(mergedListItems);

    另外请注意,您的

    itemObj.itemIndex = obj.index + '' + index;
    

    逻辑-如果两者之一 index 大于10?然后,您可能会看到一个带有 itemIndex 例如, 111 . 这是什么意思,它是指原始对象的索引是11,还是项目索引11?如果可能,考虑在它们之间放置下划线或一些分隔符,例如

    itemObj.itemIndex = obj.index + '_' + index;
    

    或者,像在我的代码中那样使用模板文本

    itemIndex: `${index}_${itemIndex}`
    
        2
  •  1
  •   Kamil Kiełczewski    6 年前

    在代码中使用

    itemObj.itemIndex = obj.index + '' + index;
    

    这对于像这样的案件来说不是唯一的 obj.index=12 index=3 obj.index=1 index=23 . 但是如果你改变 '' '-' 它将是独一无二的。

    尝试向每个items对象添加字段“id”,如下所示 id= productGroup.index + '-' + i (其中i是数组中的项索引)

    productList.forEach(pr=>pr.items.forEach((x,i)=> x.id = pr.index+'-'+i ));
    

    const productList = [{
      productGroup : 'PG1',
      index: 1,
      items: [{
        item1: 'item1 value',
        item2: 'item2 value'
      },{
        item1: 'item1 value',
        item2: 'item2 value'
      },{
        item1: 'item1 value',
        item2: 'item2 value'
      }]
    }, {
      productGroup : 'PG2',
      index: 2,
      items: [{
        item1: 'item1 value',
        item2: 'item2 value'
      },{
        item1: 'item1 value',
        item2: 'item2 value'
      }]
    }];
    
    productList.forEach(pr=>pr.items.forEach((x,i)=> x.id = pr.index+'-'+i ));
    
    console.log(productList);

    您还可以创建独立于productgroup.index的uniqe id。

    let i=0;
    productList.forEach(pr=>pr.items.forEach(x=> x.id = i++ ));
    

    const productList = [{
      productGroup : 'PG1',
      index: 1,
      items: [{
        item1: 'item1 value',
        item2: 'item2 value'
      },{
        item1: 'item1 value',
        item2: 'item2 value'
      },{
        item1: 'item1 value',
        item2: 'item2 value'
      }]
    }, {
      productGroup : 'PG2',
      index: 2,
      items: [{
        item1: 'item1 value',
        item2: 'item2 value'
      },{
        item1: 'item1 value',
        item2: 'item2 value'
      }]
    }];
    
    let i=0;
    productList.forEach(pr=>pr.items.forEach(x=> x.id = i++ ));
    
    console.log(productList);
        3
  •  0
  •   Shidersz    6 年前

    这里有一种方法 reduce() 和实验 flatMap() . 我还将把产品名称附加到新的 identifier :

    const productList = [{
      productGroup : 'PG1',
      index: 1,
      items: [
        {item1: 'item1 value', item2: 'item2 value'},
        {item1: 'item1 value', item2: 'item2 value'},
        {item1: 'item1 value', item2: 'item2 value'}
      ]
    },
    {
      productGroup : 'PG2',
      index: 2,
      items: [
        {item1: 'item1 value', item2: 'item2 value'},
        {item1: 'item1 value', item2: 'item2 value'}
      ]
    }];
    
    let newList = productList.reduce((res, curr) =>
    {
        let objs = curr.items.flatMap((x, idx) => Object.assign({
            productGroup: curr.productGroup,
            pgIndex: curr.index,
            itemIndex: [curr.productGroup, curr.index, idx].join("_")
        }, x));
        return res.concat(objs);
    }, []);
    
    console.log(newList);
        4
  •  0
  •   Spangle    6 年前

    我在下面写的代码将为您完成这项工作,并且是一个足够简单的可视化解决方案。 解决方案需要使用嵌套for循环,因为我们也在内部items数组中循环。我在下面的代码中添加了一些注释来解释正在发生的事情。

          const productList = [{
          productGroup : 'PG1',
          index: 1,
          items: [{
            item1: 'item1 value',
            item2: 'item2 value'
          },{
            item1: 'item1 value',
            item2: 'item2 value'
          },{
            item1: 'item1 value',
            item2: 'item2 value'
          }]
        }, {
          productGroup : 'PG2',
          index: 2,
          items: [{
            item1: 'item1 value',
            item2: 'item2 value'
          },{
            item1: 'item1 value',
            item2: 'item2 value'
          }]
        }];
    
        var count;
        for(var char in productList){
            count = 1; // Will reset to 1 when we go to a new product group
            for(var i = 0; i < productList[char].items.length; i++){
                // Adding in the unique key to the items.
                productList[char].items[i].uniqueKey = productList[char].productGroup + "_" + count;
                count++; 
            }   
        }
    

    我希望这有帮助:p如果你有任何问题,请告诉我。