简体   繁体   English

尝试创建 arrays 数组:data.map 不是 function

[英]Trying to create array of arrays: data.map is not a function

I have some data, that looks like this mock data:我有一些数据,看起来像这个模拟数据:

const mockData = [
  {
    "1": [
      {
        val1: 0.9323809524,
        val2: 5789.12,
        val3: 84.467,
        val4: 189.12,
        val5: 8,
        bins: 1,
      },
      {
        val1: 10.7328571429,
        val2: 5478.43,
        val3: 84.467,
        val4: -121.57,
        val5: 9,
        bins: 1,
      },
      {
        val1: 1.4457142857,
        val2: 5647.59,
        val3: 84.467,
        val4: 47.59,
        val5: 10,
        bins: 1,
      },
    ],
    "2": [
      {
        val1: 0.9323809524,
        val2: 5289.12,
        val3: 84.467,
        val4: 189.12,
        val5: 8,
        bins: 2,
      },
      {
        val1: 10.7328571429,
        val2: 5178.43,
        val3: 84.467,
        val4: -121.57,
        val5: 9,
        bins: 2,
      },
      {
        val1: 1.4457142857,
        val2: 5547.59,
        val3: 84.467,
        val4: 47.59,
        val5: 10,
        bins: 2,
      }
    ],
    "3": [
      {
        val1: 0.9323809524,
        val2: 5089.12,
        val3: 84.467,
        val4: 189.12,
        val5: 8,
        bins: 3,
      },
      {
        val1: 10.7328571429,
        val2: 5178.43,
        val3: 84.467,
        val4: -121.57,
        val5: 9,
        bins: 3,
      },
      {
        val1: 1.4457142857,
        val2: 5247.59,
        val3: 84.467,
        val4: 47.59,
        val5: 10,
        bins: 3,
      },
    ],
  },
];

So using useState I just set that to:所以使用useState我只是将其设置为:

const [data, setData] = useState(mockData)

What I want to do is iterate through this data to get 3 arrays with the values of eg val2 .我想要做的是遍历此数据以获取 3 arrays 的值,例如val2 So basically the result should be something like:所以基本上结果应该是这样的:

const newData = [
    [5789.12, 5478.43, 5647.59],
    [5289.12, 5178.43, 5547.59],
    [5089.12, 5178.43, 5247.59]
]

Now, if I try to do it a bit manually, like:现在,如果我尝试手动操作一下,例如:

console.log(Object.keys(data[1].map((x) => x.val2)).map((val) => data[1].map((x) => x.val2)[val]));

Then I get an array for the first entry.然后我得到第一个条目的数组。 But if I try to iterate through it, to actually create a new array of arrays, eg:但是如果我尝试遍历它,实际创建一个新的 arrays 数组,例如:

const [newData, setNewData] = useState([]);

setNewData(
    data.map((y, i) => ({
      Object.keys(y.map((x) => x.val2)).map((val) => y.map((x) => x.val2)[val]),   
    }))
  );

I just get the error Uncaught TypeError: y.map is not a function .我刚收到错误Uncaught TypeError: y.map is not a function I've tried to just Object.keys a few places, without luck, but I may just be confused to why it doesn't work.我试过Object.keys几个地方,没有运气,但我可能只是对为什么它不起作用感到困惑。

So what am I missing here?那我在这里错过了什么?

{
   Object.keys(y.map((x) => x.val2)).map((val) => y.map((x) => x.val2)[val]),   
}

Your map does not return values properly because of an extra pair of brackets {} .由于一对额外的括号{} ,您的map无法正确返回值。 You can check the logic below你可以检查下面的逻辑

 const data = [ { "1": [ { val1: 0.9323809524, val2: 5789.12, val3: 84.467, val4: 189.12, val5: 8, bins: 1, }, { val1: 10.7328571429, val2: 5478.43, val3: 84.467, val4: -121.57, val5: 9, bins: 1, }, { val1: 1.4457142857, val2: 5647.59, val3: 84.467, val4: 47.59, val5: 10, bins: 1, }, ], "2": [ { val1: 0.9323809524, val2: 5289.12, val3: 84.467, val4: 189.12, val5: 8, bins: 2, }, { val1: 10.7328571429, val2: 5178.43, val3: 84.467, val4: -121.57, val5: 9, bins: 2, }, { val1: 1.4457142857, val2: 5547.59, val3: 84.467, val4: 47.59, val5: 10, bins: 2, } ], "3": [ { val1: 0.9323809524, val2: 5089.12, val3: 84.467, val4: 189.12, val5: 8, bins: 3, }, { val1: 10.7328571429, val2: 5178.43, val3: 84.467, val4: -121.57, val5: 9, bins: 3, }, { val1: 1.4457142857, val2: 5247.59, val3: 84.467, val4: 47.59, val5: 10, bins: 3, }, ], }, ]; const results = data.map((item) => Object.keys(item).map(key => item[key].map((value) => value.val2))).flat(); console.log(results)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM