【问题标题】:Convert and filter dynamic JSON returned object转换和过滤动态 JSON 返回的对象
【发布时间】:2018-08-08 15:15:01
【问题描述】:

我不知道如何实现这一点,

我有这个对象,它是一个返回的 JSON:

[
    { first_dynamic_property: 'something', second_dynamic_property: 'something else' },
    { first_dynamic_property: 'something 2', second_dynamic_property: 'something else 2' },
    { first_dynamic_property: 'something 3', second_dynamic_property: 'null' },
    { first_dynamic_property: 'something', second_dynamic_property: 'something else' },
]

我想把这个对象转换成一个新的对象,比如:

[
    { first_dynamic_property: 'something' },
    { first_dynamic_property: 'something 2' },
    { first_dynamic_property: 'something 3' },
    { second_dynamic_property: 'something else' }
]

如果你看,该对象过滤了重复的对象并将两项转换为一项对象。我不记得如何实现这一点。你知道最好的方法吗?

感谢和问候。

编辑 1:

我使用了 first_dynamic_propertysecond_dynamic_property,因为我不知道键名或它们的数量.它们是动态生成的。

【问题讨论】:

    标签: javascript arrays json ecmascript-6


    【解决方案1】:

    这是用于转换任何对象列表的通用代码:

    const data = [
        { first_dynamic_property: 'something', second_dynamic_property: 'something else' },
        { first_dynamic_property: 'something 2', second_dynamic_property: 'something else 2' },
        { first_dynamic_property: 'something 3', second_dynamic_property: 'null' },
        { first_dynamic_property: 'something', second_dynamic_property: 'something else' },
    ];
    const result = data.reduce((acc, item) => {
            return [...acc, ...Object.keys(item).map((key) => { return {[key]: item[key]} })];
      }, []
    );
    console.log(result[0]);
    

    希望这会有所帮助:)

    更新:如果您只需要显示唯一元素,您可以添加计数器映射和过滤数据。像这样的:

    const values_counter = {};
    const result = data.reduce((acc, item) => {
            return [...acc, ...Object.keys(item).map(key => { 
            values_counter[item[key]] = values_counter[item[key]] + 1 || 1;
          return {[key]: item[key]} })
        ];
      }, []
    ).filter(item => values_counter[item[Object.keys(item)[0]]] === 1);
    

    【讨论】:

      【解决方案2】:

      根据新要求更新:

      let dataList = [
          { first_dynamic_property: 'something', second_dynamic_property: 'something else' },
          { first_dynamic_property: 'something 2', second_dynamic_property: 'something else 2' },
          { first_dynamic_property: 'something 3', second_dynamic_property: 'null' },
          { first_dynamic_property: 'something', second_dynamic_property: 'something else' },
      ]
      let values = new Set();
      
      let result = dataList.map(data => {
          let newData = {};
          for (let key in data) {
              let value = data[key];
              if (values.has(value)) continue;
              values.add(value);
              newData[key] = value;
              break;
          }
          return newData;
      })
      
      console.info(result)
      

      【讨论】:

        【解决方案3】:

        您可以使用一个对象来存储预先存在的键,然后使用一个简单的映射函数来遍历这些键并返回新数组。

        let existingKeys = {};
        
        const input = [
            { first_dynamic_property: 'something', second_dynamic_property: 'something else' },
            { first_dynamic_property: 'something 2', second_dynamic_property: 'something else 2' },
            { first_dynamic_property: 'something 3', second_dynamic_property: 'null' },
            { first_dynamic_property: 'something', second_dynamic_property: 'something else' },
        ];
        
        let output = input.map(elem => {
            const {first_dynamic_property, second_dynamic_property} = elem;
            if(!(first_dynamic_property in existingKeys)){
                existingKeys[first_dynamic_property] = true;
                return {first_dynamic_property};
            } else if(!(second_dynamic_property in existingKeys)){
                existingKeys[second_dynamic_property] = true;
                return {second_dynamic_property};
            }
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-02-13
          • 2015-01-06
          • 2018-12-09
          • 1970-01-01
          • 2015-05-21
          • 2019-04-18
          • 2019-12-09
          • 2015-03-08
          相关资源
          最近更新 更多