转换和过滤动态JSON返回的对象

时间:2018-08-08 15:15:01

标签: javascript arrays json ecmascript-6

我不确定如何实现这一目标,

我有这个对象,它是返回的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_property second_dynamic_property ,因为我不知道键名或键名多少。它们是动态生成的。

3 个答案:

答案 0 :(得分:1)

根据新要求进行了更新:

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)

答案 1 :(得分: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 :(得分:-1)

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

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};
    }
});