【问题标题】:Array of objects to object using properties from the objects [closed]使用来自对象的属性的对象数组[关闭]
【发布时间】:2023-03-15 22:17:01
【问题描述】:

对不起,如果这有点令人困惑,但我有这个数组:

[
      {
        name: 'id',
        type: 'string',
        primary: true
      },
      {
        name: 'notes',
        type: 'text',
        default: '[]'
      },
      {
        name: 'reminders',
        type: 'text',
        default: '[]'
      }
    ]

我怎么把它变成

{
  notes: '[]',
  reminders: '[]'
}

?逻辑如下:任何具有default属性的索引,将新对象中的键设置为索引的name,并将值设置为索引的default

【问题讨论】:

    标签: javascript arrays node.js object


    【解决方案1】:

    使用reduce:

    const arr = [{
        name: 'id',
        type: 'string',
        primary: true
      },
      {
        name: 'notes',
        type: 'text',
        default: '[]'
      },
      {
        name: 'reminders',
        type: 'text',
        default: '[]'
      }
    ];
    
    const obj = arr.reduce((acc, curr) => curr.default ? { ...acc,
      [curr.name]: curr.default
    } : acc);
    
    console.log(obj);

    【讨论】:

    • 这种方法的结果是{name: "id", type: "string", primary: true, notes: "[]", reminders: "[]"} 而不是OP要求的
    【解决方案2】:

    const data = [
          {
            name: 'id',
            type: 'string',
            primary: true
          },
          {
            name: 'notes',
            type: 'text',
            default: '[]'
          },
          {
            name: 'reminders',
            type: 'text',
            default: '[]'
          }
    ];
    
    const result = Object.fromEntries(
      data
        .filter(it => "default" in it)
        .map(it => ([it.name, it.default]))
    );
    
    console.log(result);

    【讨论】:

      【解决方案3】:

      您可以将filterObject.keys() 一起使用。

      // Your initial array
      const initialArray = [{
              name: 'id',
              type: 'string',
              primary: true
          },
          {
              name: 'notes',
              type: 'text',
              default: '[]'
          },
          {
              name: 'reminders',
              type: 'text',
              default: '[]'
          }
      ];
      
      // Array just with items that haves a default value
      const itensWithDefaultValue = initialArray.filter(item => Object.keys(item).includes('default'));
      
      // Object to save items 
      let objectWithValues = {};
      
      // Save items in object with name and you default value
      itensWithDefaultValue.map(item => objectWithValues[item.name] = item.default);
      
      console.log(objectWithValues);

      希望这会有所帮助!

      【讨论】:

        【解决方案4】:

        你可以用 reduce 来实现

        var array = [
              {
                name: 'id',
                type: 'string',
                primary: true
              },
              {
                name: 'notes',
                type: 'text',
                default: '[]'
              },
              {
                name: 'reminders',
                type: 'text',
                default: '[]'
              }
            ]
        
        var result = array.reduce((prev, el) => {
           if (el.default) {
               prev[el.name] = el.default
           }
           return prev;
        },{})
        
        console.log(result);

        【讨论】:

          【解决方案5】:

          您可以将新对象分散到单个对象中。

          var data = [{ name: 'id', type: 'string', primary: true }, { name: 'notes', type: 'text', default: '[]' }, { name: 'reminders', type: 'text', default: '[]' }],
              result = Object.assign({}, ...data.map(({ name, default: d }) => d && { [name]: d }));
          
          console.log(result);

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-02-08
            • 1970-01-01
            • 2021-11-08
            • 1970-01-01
            • 1970-01-01
            • 2020-01-10
            • 1970-01-01
            • 2023-02-09
            相关资源
            最近更新 更多