如何称呼那些拥有更多数据的人? json

时间:2019-03-05 21:16:23

标签: javascript json reactjs

美好的一天,因为我可以从json数组中调用包含更多数据的对象,所以我正在创建一个图,并且必须包含包含更多数据的对象,因此我将这段代码发送到我的数据中

vehiculoPorColores = () => {
const _this = this
fetch("/live/graph/color")
  .then(response => response.json())
  .then(datos => {
    const vehicleKey = Object.keys(datos)
    console.log(vehicleKey)
    _this.setState(
      {
        vehiculosC: vehicleKey.splice(0, 5).map((i, e) => ({
          name: vehicleKey[e],
          colores: datos[i]
        }))
      },
      () => {
        setTimeout(this.vehiculoPorColores, 1000)
      }
    )
  })
  }

JSON:

{
  "lavender": 3,
  "white": 12,
  "dimgrey": 4,
  "seagreen": 2,
  "lightslategrey": 6,
  "slategrey": 7,
  "indianred": 2,
  "snow": 1,
  "darkslategrey": 1,
  "khaki": 1,
  "gainsboro": 3,
  "palevioletred": 1,
  "darkgrey": 1
}

更具体地说:json

我需要对数据进行排序并选择具有最高值的5条记录,现在我只向5发送呼叫,但它们不是具有更多数据的记录,我需要它并且它们仅是5 aahh和我一起工作

1 个答案:

答案 0 :(得分:0)

如果要在JS中对数组进行排序,则可以...

具有一个形状如下的对象: {  key1:10,  key2:“一个字符串” }

然后有一个数组:

router.post('/nvdGenerate', (req, res) => {
  console.log('length', req.body.length);
  var listOfObjectToAdd = [];
  for (let i=0; i< req.body.length; i++)
  {
    let obj = {
       ID :req.body[i].ID,
       affects : req.body[i].affects,
       impact : req.body[i].impact
    };
    listOfObjectToAdd.push(obj);
  };
  console.log('trying to insert documents');
  nvdSchema.insertMany(listOfObjectToAdd, function(err) {
    if (err) console.error(err);
    console.log('finish!');
  //  res.send('finish!');
  });
})

您可以这样做:

[{
 key1: 10,
 key2: "a string"
},
{
 key1: 20,
 key2: "a string"
}]

和更大的第一:

[{
 key1: 10,
 key2: "a string"
},
{
 key1: 20,
 key2: "a string"
}].sort((elemN, elemNplus)=> elemN.key1 - elemNplus.key1) :: output => [{
 key1: 10,
 key2: "a string"
},
{
 key1: 20,
 key2: "a string"
}]