如何从推入平面数组的嵌套数组中获取值?

How do I get values from a nested array pushed into a flat array?

我有一个 api 返回这样的数据集:

const data = {
  session: ....,
  timestamp: ....,
  samples: [
    {
      key: 'I',
      values: [
        { timing: '12356timingdatething', reading: -37.1234 },
        { timing: '12356timingdatething', reading: -32.1234 },
        { timing: '12356timingdatething', reading: 1.1234 },
        // ....
      ],
    },
    {
      key: 'I',
      values: [
        { timing: '12356timingdatething', reading: -100.1234 },
        { timing: '12356timingdatething', reading: 5.1234 },
        { timing: '12356timingdatething', reading: 5.3334 },
        // ....
      ],
    },
    {
      key: 'I',
      values: [
        { timing: '12356timingdatething', reading: -37.1234 },
        { timing: '12356timingdatething', reading: -32.1234 },
        { timing: '12356timingdatething', reading: 1.1234 },
        // ....
      ],
    },
  ]
}

我想做的是从 samples 抓取 values 并将它们推入一个新数组,该数组将只是一个平面数组,如下所示:

const newData = [
  [
    { timing: '12356timingdatething', reading: -37.1234 },
    { timing: '12356timingdatething', reading: -32.1234 },
    { timing: '12356timingdatething', reading: 1.1234 },
    // ....
    { timing: '12356timingdatething', reading: -100.1234 },
    { timing: '12356timingdatething', reading: 5.1234 },
    { timing: '12356timingdatething', reading: 5.3334 },
    // ....
    { timing: '12356timingdatething', reading: -37.1234 },
    { timing: '12356timingdatething', reading: -32.1234 },
    { timing: '12356timingdatething', reading: 1.1234 },
    // ....
  ]

我能够像这样将 samples 放入一个新数组中:

this.setState(prevState => ({
  newArray: [...prevState.newArray, [...data.samples] ]
}));

我愿意使用像 lodashramda 这样的库,如果有一个库可以使这更容易的话。

您可以使用 reduce()flatMap():

const newData = data.reduce((accumulated, current) => [...accumulated, ...current.values], []);

或者:

const newData = data.flatMap(item => item.values);

你可以利用.reduce():

let ans = data.samples.reduce((cum,x)=>{
 return [...cum,...(x.values)];   
},[])

使用 Ramda,您可以使用 R.chainR.prop 来使值变平所有 values:

const fn = R.chain(R.prop('values'))

const data = {"samples":[{"key":"I","values":[{"timing":"12356timingdatething","reading":-37.1234},{"timing":"12356timingdatething","reading":-32.1234},{"timing":"12356timingdatething","reading":1.1234}]},{"key":"I","values":[{"timing":"12356timingdatething","reading":-100.1234},{"timing":"12356timingdatething","reading":5.1234},{"timing":"12356timingdatething","reading":5.3334}]},{"key":"I","values":[{"timing":"12356timingdatething","reading":-37.1234},{"timing":"12356timingdatething","reading":-32.1234},{"timing":"12356timingdatething","reading":1.1234}]}]}

const result = fn(data.samples)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js" integrity="sha512-rZHvUXcc1zWKsxm7rJ8lVQuIr1oOmm7cShlvpV0gWf0RvbcJN6x96al/Rp2L2BI4a4ZkT2/YfVe/8YvB2UHzQw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

你可以使用 lodash _.flatMap():

const data = {"samples":[{"key":"I","values":[{"timing":"12356timingdatething","reading":-37.1234},{"timing":"12356timingdatething","reading":-32.1234},{"timing":"12356timingdatething","reading":1.1234}]},{"key":"I","values":[{"timing":"12356timingdatething","reading":-100.1234},{"timing":"12356timingdatething","reading":5.1234},{"timing":"12356timingdatething","reading":5.3334}]},{"key":"I","values":[{"timing":"12356timingdatething","reading":-37.1234},{"timing":"12356timingdatething","reading":-32.1234},{"timing":"12356timingdatething","reading":1.1234}]}]}

const result = _.flatMap(data.samples, 'values')

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>