从嵌套数组中获取可用数字 Json

Getting usable numbers from Nested Array Json

我得到的数据公式如下。我正在努力使用 reduce 或 map

从中获取任何可用数据
const data = [
  {
    id: 25,
    status: 1,
    description: "No Description",
    length: 4,
    data: [
      {
        id: 43,
        comment: "Comment1",
        eventTimestamp: 1541027189000,
        intensity: 29
      },
      {
        comment: "Comment2",
        eventTimestamp: 1541027191000,
        intensity: 33
      },
      {
        id: 45,
        comment: "Comment3",
        eventTimestamp: 1541027193000,
        intensity: 30
      }
    ],
    tTypes: [
      {
        id: 3,
        label: "Johnny",
        certainty: "TEST",
        comment: "Test Purposes Only",
        icon: "bottle",
        number: 0
      }
    ]
  }
];

我试过扁平化,我试过迭代 JSON 两次,我似乎只是以“NaN”或未定义结束。我希望能够按时间顺序(使用时间戳)对它们进行排序,从强度值中获取 mix/max/ave 等等。我已经计算出更高一级的长度,但似乎无法计算出其余部分。有人能指出我正确的方向吗?

export default function App() {
  let tTypesArray = data.map((a) => a.tTypes);
  let Walker = tTypesArray.reduce((a, tTypes) => tTypes.label === "Johnny" ? ++a : a, 0);

  console.log(Walker);
  console.log(tTypesArray[0].label);
  console.log([].concat(...data)
  .map(data => data.tTypes.number)
  .reduce((a, b) => a + b))

  console.log([].concat(...data).reduce((a, { tTypes: { id }}) => id, 0))

  return <div className="App">ARG!</div>;
}

是我试过的一些例子。 https://codesandbox.io/s/purple-cache-ivz1y?file=/src/App.js link 是沙盒。

我从你的问题中了解到,你需要循环 data 并且对于 data 中的每个元素你想要提取值并进行一些计算。

首先你需要循环你的数据输入。我将使用 Array.forEach:

data.forEach(element => { ... })

现在我们有了一个循环,我们可以访问每个元素 属性 并提取我们想要的信息。例如,假设您想 sort 按时间戳升序排列的评论:

const sortedComments = element.data.sort((a, b) => a.eventTimestamp - b.eventTimestamp);

console.log(sortedComments)

现在假设您想要评论中的最小、最大和平均强度。有几种方法可以得到它。这是一个算法:

let min = Infinity;
let max = -Infinity;
let sum = 0;

for(comment of sortedComments) {
  if(comment.intensity < min) {
    min = comment.intensity;
  }

  if(comment.intensity > max) {
    max = comment.intensity;
  }

  sum += comment.intensity;
}

const avg = sum / sortedComments.length;

console.log({min, max, avg})

综合起来:

const data = [
  {
    id: 25,
    confirmationStatus: 1,
    description: "No Description",
    length: 4,
    data: [
      {
        id: 43,
        comment: "Comment1",
        eventTimestamp: 1541027189000,
        intensity: 29
      },
      {
        comment: "Comment2",
        eventTimestamp: 1541027191000,
        intensity: 33
      },
      {
        id: 45,
        comment: "Comment3",
        eventTimestamp: 1541027193000,
        intensity: 30
      }
    ],
    tTypes: [
      {
        id: 3,
        label: "Johnny",
        certainty: "TEST",
        comment: "Test Purposes Only",
        icon: "bottle",
        number: 0
      }
    ]
  }
];


data.forEach(element => {
  const sortedComments = element.data.sort((a, b) => a.eventTimestamp - b.eventTimestamp);

  console.log(sortedComments);
  let min = Infinity;
  let max = -Infinity;
  let sum = 0;

  for(comment of sortedComments) {
    if(comment.intensity < min) {
      min = comment.intensity;
    }

    if(comment.intensity > max) {
      max = comment.intensity;
    }

    sum += comment.intensity;
  }

  const avg = sum / sortedComments.length;

  console.log({min, max, avg});

  let walker = element.tTypes.reduce(
    (a, tType) => (tType.label === "Johnny" ? ++a : a), 0
  );

  console.log(walker)
});

我希望它能让你朝着正确的方向前进。