如何列出与数组中的键相等的json对象的值?

时间:2017-11-21 18:41:37

标签: javascript json

const columns = ['dataA', 'dataD', 'dataB', 'dataE'];

const data = [{
    dataA: '001a',
    dataB: '123456',
    dataC: 'true',
    dataD: 'normal',
    dataE: 'ON'
  },
  {
    dataA: '002a',
    dataB: '123456',
    dataC: 'true',
    dataD: 'exception',
    dataE: 'ON'
  },
  {
    dataA: '003a',
    dataB: '098767',
    dataC: 'true',
    dataD: 'normal',
    dataE: 'OFF'
  },
  {
    dataA: '001a',
    dataB: '567890',
    dataC: 'false',
    dataD: 'normal',
    dataE: 'ON'
  }
];

const result = data.map(a => columns.map(k => {
  return {
    [k]: a[k]
  };
}));

console.log(result);

我设法过滤了值,但现在每个键和值都是一个对象。我如何修改代码,以便获得与数据结构相同的结果?

我需要结果采用这种格式:

[
  { dataA: "001a", dataD: "normal", dataB: "123456", dataE: "ON" }
  { dataA: "002a", dataD: "exception", dataB: "123456", dataE: "ON" }
  { dataA: "003a", dataD: "normal", dataB: "098767", dataE: "OFF" }
  { dataA: "001a", dataD: "normal", dataB: "567890", dataE: "ON" }
]

3 个答案:

答案 0 :(得分:3)

将内部地图的结果传播到Object#assign将它们组合到一个对象:



const columns = ['dataA', 'dataD', 'dataB', 'dataE'];

const data = [{"dataA":"001a","dataB":"123456","dataC":"true","dataD":"normal","dataE":"ON"},{"dataA":"002a","dataB":"123456","dataC":"true","dataD":"exception","dataE":"ON"},{"dataA":"003a","dataB":"098767","dataC":"true","dataD":"normal","dataE":"OFF"},{"dataA":"001a","dataB":"567890","dataC":"false","dataD":"normal","dataE":"ON"}];

const result = data.map(a => Object.assign(...columns.map(k => ({
  [k]: a[k]
}))));

console.log(result);




或使用Array#reduce而不是map:

创建对象



const columns = ['dataA', 'dataD', 'dataB', 'dataE'];

const data = [{"dataA":"001a","dataB":"123456","dataC":"true","dataD":"normal","dataE":"ON"},{"dataA":"002a","dataB":"123456","dataC":"true","dataD":"exception","dataE":"ON"},{"dataA":"003a","dataB":"098767","dataC":"true","dataD":"normal","dataE":"OFF"},{"dataA":"001a","dataB":"567890","dataC":"false","dataD":"normal","dataE":"ON"}];

const result = data.map(a => columns.reduce((r, k) => (r[k] = a[k], r), {}));

console.log(result);




答案 1 :(得分:2)

你走在正确的道路上。请注意,对象属性本质上是无序的,不要依赖于它们按特定顺序排列。



const columns = ['dataA', 'dataD', 'dataB', 'dataE'];

const data = [{ dataA: '001a', dataB: '123456', dataC: 'true', dataD: 'normal', dataE: 'ON' }, { dataA: '002a', dataB: '123456', dataC: 'true', dataD: 'exception', dataE: 'ON' }, { dataA: '003a', dataB: '098767', dataC: 'true', dataD: 'normal', dataE: 'OFF' }, { dataA: '001a', dataB: '567890', dataC: 'false', dataD: 'normal', dataE: 'ON' }];

const result = data.map(a => columns.reduce((acc, curr) => ({
  ...acc, [curr]: a[curr]
}), {}))

console.log(result);

/*
[
  {
    "dataA": "001a",
    "dataD": "normal",
    "dataB": "123456",
    "dataE": "ON"
  },
  {
    "dataA": "002a",
    "dataD": "exception",
    "dataB": "123456",
    "dataE": "ON"
  },
  {
    "dataA": "003a",
    "dataD": "normal",
    "dataB": "098767",
    "dataE": "OFF"
  },
  {
    "dataA": "001a",
    "dataD": "normal",
    "dataB": "567890",
    "dataE": "ON"
  }
]
*/




答案 2 :(得分:1)

而不是内部map,您需要reduce到对象上:

const result = data.map(a => columns.reduce((obj, k) => {
  obj[k] = a[k];
  return obj;
}, {}));

请注意,对象属性没有固有的顺序。