如何通过两个相同的键按json数组分组

时间:2019-03-11 07:06:10

标签: javascript jquery

如何基于两个json键f和b进行分组 我的json数组

var json=[
    {   s:'s',  f:1,  b:1,  q:2   },
    {   s:'s',  f:1,  b:1,  q:3   },
    {   s:'s',  f:2,  b:1,  q:2   },
    {   s:'s',  f:2,  b:1,  q:2   },
    {   s:'s',  f:1,  b:2,  q:2   },
    {   s:'s',  f:1,  b:2,  q:2   },
    {   s:'s',  f:0,  b:1,  q:2   },
    {   s:'s',  f:0,  b:1,  q:2   },
    {   s:'s',  f:1,  b:0,  q:2    },
    {   s:'s',  f:1,  b:0, q:2    },
    {   s:'s',  f:0,  b:0,  q:2    },
    {   s:'s',  f:0,  b:0, q:2    },

];

预期产量

 var op=[
    {   s:'s',  f:1,  b:1,  q:5   },
    {   s:'s',  f:2,  b:1,  q:4   },
    {   s:'s',  f:1,  b:2,  q:4   },
    {   s:'s',  f:0,  b:1,  q:4   },
    {   s:'s',  f:1,  b:0,  q:4   },
    {   s:'s',  f:0,  b:0,  q:4   },

];

1 个答案:

答案 0 :(得分:0)

import React, { useState } from 'react';
import "./timer.css";

const convertToSeconds = (sec) => {
  let divisor_for_minutes = sec % (60 * 60);
  var minutes = Math.floor(divisor_for_minutes / 60);

  let divisor_for_seconds = divisor_for_minutes % 60;
  var seconds = Math.ceil(divisor_for_seconds);

  var obj = {"m": minutes, "s": seconds};
  return obj;
}

const Timer= () => {
  const [seconds, setSeconds] = useState(5)
  const [time, setTime] = useState(convertToSeconds(seconds))
  const [color, setColor] = useState('darkgrey')
  const [timer, setTimer] = useState(0)


  const startTimer = () => {
    // If timer is not set, set the interval
    if(timer === 0 && seconds > 0){
        // 1000 ms = 1 second
        setTimer(setInterval(startCountDown, 1000))
    }
  }

  const startCountDown = () => {
        let newSeconds = seconds - 1;
        if(newSeconds >= 0){
          if(newSeconds<=2){
            setColor('red')
          }
            setTime(convertToSeconds(newSeconds))
            setSeconds(newSeconds)
      }
    else{
        setTime(convertToSeconds(5))
        setSeconds(5)
        setColor('darkgrey')
        setTimer(0)
    }
  }

  return(
    <div className="component-timer">
      <div class="startbtn">
          <button onClick={startTimer}>Start</button>
      </div>

        <div class="timer"><div style = {{color}}>
          m: {time.m} s: {time.s}
        </div>
      </div>
    </div>
  )   
}

export default Timer