代码之家  ›  专栏  ›  技术社区  ›  Damian C

使用其他数组中的值创建新数组

  •  -1
  • Damian C  · 技术社区  · 1 年前

    为了防止混淆,我简化了下面的数组。以下是我到目前为止的进展。。。我尝试过各种方法来生成新的数组,但都没有成功。我甚至在Stack Overflow上搜索过解决方案。结果是准确的,但不幸的是,我在字段数组中只接收到一个对象。我相信我目前的方法已经接近解决方案。如能提供任何帮助,我们将不胜感激。非常感谢。

    我有以下数组。

    [
        {
            fileName: 'Home_AIT_Spraying-1_29062023.agdata',
            fileType: 'AgData',
            metadata: {
                tags: [
                    {
                        type: 'Field',
                        value: 'Home',
                        uniqueId: null,
                    },
                    {
                        type: 'Farm',
                        value: 'OTF',
                        uniqueId: null,
                    },
                    {
                        type: 'Grower',
                        value: 'Jim Smith',
                        uniqueId: null,
                    }
                ],
            },
        },
        {
            fileName: 'AIT_AIT_Spraying-1_30062023.agdata',
            fileType: 'AgData',
            metadata: {
                tags: [
                    {
                        type: 'Field',
                        value: 'Oscar',
                        uniqueId: null,
                    },
                    {
                        type: 'Farm',
                        value: 'OTF',
                        uniqueId: null,
                    },
                    {
                        type: 'Grower',
                        value: 'Jasper Jones',
                        uniqueId: null,
                    }
                ],
            },
        }
    ]
    

    我希望输出如下所示:

    [
        {
            Field: 'Home',
            Farm: 'OTF',
            Grower: 'Jim Smith',
        },
        {
            Field: 'Oscar',
            Farm: 'OTF',
            Grower: 'Jasper Jones',
        },
    ];
    
    

    以及我目前的解决方案:

    const fieldsMetaArray = [] as [];
    
    data.forEach((val, key) => {
        console.log('key', key);
        val.metadata.tags.map((tag) => {
            console.log(`key: ${tag.type} value: ${tag.value}`);
            fieldsMetaArray[tag.type] = tag.value;
        });
    });
    
    console.log(fieldsArray);
    
    
    3 回复  |  直到 1 年前
        1
  •  4
  •   Jaromanda X    1 年前

    使用 Array::map x2和 Object.fromEntries 使解决方案变得非常简单——事实上,它只是一行代码(为了可读性,在此处拆分)

    const input = [ { fileName: 'Home_AIT_Spraying-1_29062023.agdata', fileType: 'AgData', metadata: { tags: [ { type: 'Field', value: 'Home', uniqueId: null, }, { type: 'Farm', value: 'OTF', uniqueId: null, }, { type: 'Grower', value: 'Jim Smith', uniqueId: null, } ], }, }, { fileName: 'AIT_AIT_Spraying-1_30062023.agdata', fileType: 'AgData', metadata: { tags: [ { type: 'Field', value: 'Oscar', uniqueId: null, }, { type: 'Farm', value: 'OTF', uniqueId: null, }, { type: 'Grower', value: 'Jasper Jones', uniqueId: null, } ], }, } ];
    
    
    const result = input.map(({ metadata: { tags } }) =>
        Object.fromEntries(tags.map(({ type, value }) => [type, value]))
    );
    
    
    console.log(result);
        2
  •  1
  •   Joshua George    1 年前

    您需要为字段MetaArray中的每个项创建一个对象,而不是一个数组,然后将每个对象推入字段MetaArray,而不是直接为其属性赋值。

    由于您希望输出仅包括“Field”、“Farm”和“Grower”,因此应在映射过程中筛选出其他类型。

    const fieldsMetaArray = [];
    
    data.forEach((val) => {
        const fieldMeta = {}; // Create a new object for each item
        val.metadata.tags.forEach((tag) => {
            // Check if the tag type is 'Field', 'Farm', or 'Grower'
            if (['Field', 'Farm', 'Grower'].includes(tag.type)) {
                // Assign the value to the corresponding property in the object
                fieldMeta[tag.type] = tag.value;
            }
        });
        // Push the object into the fieldsMetaArray
        fieldsMetaArray.push(fieldMeta);
    });
    
    console.log(fieldsMetaArray);
    
        3
  •  0
  •   mat.twg    1 年前

    let arr = [
      {
        fileName: 'Home_AIT_Spraying-1_29062023.agdata',
        fileType: 'AgData',
        metadata: {
          tags: [
            {
              type: 'Field',
              value: 'Home',
              uniqueId: null,
            },
            {
              type: 'Farm',
              value: 'OTF',
              uniqueId: null,
            },
            {
              type: 'Grower',
              value: 'Jim Smith',
              uniqueId: null,
            },
            {
              type: 'Product',
              value: 'Water',
              uniqueId: null,
            },
          ],
        },
      },
      {
        fileName: 'AIT_AIT_Spraying-1_30062023.agdata',
        fileType: 'AgData',
        metadata: {
          tags: [
            {
              type: 'Field',
              value: 'Oscar',
              uniqueId: null,
            },
            {
              type: 'Farm',
              value: 'OTF',
              uniqueId: null,
            },
            {
              type: 'Grower',
              value: 'Jasper Jones',
              uniqueId: null,
            },
            {
              type: 'Product',
              value: 'Water',
              uniqueId: null,
            },
          ],
        },
      },
    ];
    
    console.log(
      arr
        .map((entity) => entity.metadata.tags)
        .map((tags) =>
          Object.assign({}, ...tags.map((tag) => ({ [tag.type]: tag.value }))),
        ),
    );