如何为每个对象的属性创建数组?

时间:2019-02-06 12:39:28

标签: javascript

我发现了多个类似的问题,但无法获得以下解决方法:

我从服务器获取了这类数据:

test_data = {
        '222': {
            joy: 66.5057373046875,
            fear: 1.0003513832343742,
            anger: 2.000018799044483,
            disgust: 3.004251452162862,
            sadness: 4.0001135088386945,
            contempt: 5.001299204188399,
            surprise: 6.203749045729637
        },
        '238': {
            joy: 97.06363677978516,
            fear: 17.500137131541123,
            anger: 27.00000593749519,
            disgust: 6.001324078417383,
            sadness: 21.000043172625737,
            contempt: 21.00033742573578,
            surprise: 32.62530106306076
        },
        '722': {
            joy: 66.5057373046875,
            fear: 60.000351383234374,
            anger: 70.00001879904448,
            disgust: 10.004251452162862,
            sadness: 92.0001135088387,
            contempt: 40.0012992041884,
            surprise: 50.20374904572964
        }
    }

我需要为test_data对象内的每个对象创建一个数组,例如,joyArray以容纳所有joy值,fearArray所有{{1 }}值等。示例:

fear

我不确定该怎么做,我尝试使用joyArray = [66.5057373046875, 97.06363677978516, 66.5057373046875] ,但没有做到。有什么建议吗?

2 个答案:

答案 0 :(得分:3)

您可以像这样使用reduceObject.valuesObject.entries

const test_data = {'222':{joy:66.5057373046875,fear:1.0003513832343742,anger:2.000018799044483,disgust:3.004251452162862,sadness:4.0001135088386945,contempt:5.001299204188399,surprise:6.203749045729637},'238':{joy:97.06363677978516,fear:17.500137131541123,anger:27.00000593749519,disgust:6.001324078417383,sadness:21.000043172625737,contempt:21.00033742573578,surprise:32.62530106306076},'722':{joy:66.5057373046875,fear:60.000351383234374,anger:70.00001879904448,disgust:10.004251452162862,sadness:92.0001135088387,contempt:40.0012992041884,surprise:50.20374904572964}}

const output = Object.values(test_data).reduce((r, v) => {
  Object.entries(v).forEach(([k, v]) => (r[k] = r[k] || []).push(v))
  return r;
}, {})

console.log(output)

这会创建一个对象,其中test_data中的每个键都带有值数组

答案 1 :(得分:0)

最简单的方法可能是:

let test_data = {
    '222': {
      joy: 66.5057373046875,
      fear: 1.0003513832343742,
      anger: 2.000018799044483,
      disgust: 3.004251452162862,
      sadness: 4.0001135088386945,
      contempt: 5.001299204188399,
      surprise: 6.203749045729637
    },
    '238': {
      joy: 97.06363677978516,
      fear: 17.500137131541123,
      anger: 27.00000593749519,
      disgust: 6.001324078417383,
      sadness: 21.000043172625737,
      contempt: 21.00033742573578,
      surprise: 32.62530106306076
    },
    '722': {
      joy: 66.5057373046875,
      fear: 60.000351383234374,
      anger: 70.00001879904448,
      disgust: 10.004251452162862,
      sadness: 92.0001135088387,
      contempt: 40.0012992041884,
      surprise: 50.20374904572964
    }
  },

  // we retrieve an Array of the nested Objects
  // (the Object-values) using Object.values(), and
  // and then use Array.prototype.map() to iterate
  // over that Array of values:
  joyArray = Object.values(test_data).map(

    // here we pass the current Object-value into
    // the Arrow function (emotionsObject is reference
    // to that current Object; here we (implicitly)
    // return the value held in the 'joy' property:
    (emotionsObject) => emotionsObject.joy
  );

console.log(joyArray);

JS Fiddle demo

参考文献: