将对象的前n个属性减少/解构为新对象

时间:2017-09-29 11:51:47

标签: javascript

我正在尝试返回对象的前n个属性,我不在乎他们应该"应该"在(数字,字母等)。我目前正在将这n个属性作为数组返回,但是必须顺风管理这个数组会让人讨厌。

示例输入数据集:

{
      "0.00530016": "0.18990380",
      "0.00531201": "0.39754144",
      "0.00531202": "15.93707742",
      "0.00531454": "7.17396155",
      "0.00531512": "0.37666027",
      "0.00533384": "83.98047315",
      "0.00535753": "2.74703930",
      "0.00535997": "1.13950885",
      "0.00536000": "53.12723251",
      "0.00537968": "5.27397661",
      "0.00539234": "3.63687246"
}

返回前5个会产生一个看起来像这样的新对象:

{
      "0.00530016": "0.18990380",
      "0.00531201": "0.39754144",
      "0.00531202": "15.93707742",
      "0.00531454": "7.17396155",
      "0.00531512": "0.37666027"
}

我目前通过此代码将这些值返回到数组中的解决方案:

foo.property = Object.entries(dataset).slice(0, depth);

产生以下结果:

[
        [
            "0.00530016",
            "0.18990380"
        ],
        [
            "0.00531201",
            "0.39754144"
        ],
        [
            "0.00531202",
            "15.93707742"
        ],
        [
            "0.00531454",
            "7.17396155"
        ],
        [
            "0.00531512",
            "0.37666027"
        ]
]

那么,我怎样才能将我已经拥有的数组变为示例对象,或者如何在没有此数组的情况下首先返回该示例对象?

我尝试在现有代码的末尾使用reduce,但我在这里有点缺乏经验,所以我认为我做错了,因为这需要是动态的。

4 个答案:

答案 0 :(得分:1)

Javascript对象没有订单,但由于您按照排序顺序引用它们,您可以这样做

let obj = {
      "0.00530016": "0.18990380",
      "0.00531201": "0.39754144",
      "0.00531202": "15.93707742",
      "0.00531454": "7.17396155",
      "0.00531512": "0.37666027",
      "0.00533384": "83.98047315",
      "0.00535753": "2.74703930",
      "0.00535997": "1.13950885",
      "0.00536000": "53.12723251",
      "0.00537968": "5.27397661",
      "0.00539234": "3.63687246"
}

let result = Object.keys(obj).sort((a, b) => a-b).slice(0, 5).reduce((a, b) => {
    a[b] = obj[b];
    return a;
}, {});

console.log(result);

如果您不希望订单重要,只需删除排序

答案 1 :(得分:1)

只需使用循环复制属性并在n个属性后突破:

function getSomeProperties(obj, N) {
    var res = {};
    var i = 0;
    for (var p in obj) {
        if (i++ >= N) return res;
        res[p] = obj[p];
    }
    return res; // or throw an error that there were less than N properties in obj
}

答案 2 :(得分:0)

  

我试图返回对象的前n个属性,我不知道   关心他们应该遵循什么样的顺序"在(数字,字母等)。

如果订单不重要(这意味着您将获取任何5个属性),那么试试这个



var depth = 5;
var obj = {
      "0.00530016": "0.18990380",
      "0.00531201": "0.39754144",
      "0.00531202": "15.93707742",
      "0.00531454": "7.17396155",
      "0.00531512": "0.37666027",
      "0.00533384": "83.98047315",
      "0.00535753": "2.74703930",
      "0.00535997": "1.13950885",
      "0.00536000": "53.12723251",
      "0.00537968": "5.27397661",
      "0.00539234": "3.63687246"
};
var output={}; 
Object.keys( obj ).slice( 0, depth ).forEach( function( key ){ output[ key ] = obj[ key ] });
console.log( JSON.stringify( output, 0, 4 ) );




答案 3 :(得分:0)

这将mutate原始dataset使用delete



var dataset = {
  "0.00530016": "0.18990380",
  "0.00531201": "0.39754144",
  "0.00531202": "15.93707742",
  "0.00531454": "7.17396155",
  "0.00531512": "0.37666027",
  "0.00533384": "83.98047315",
  "0.00535753": "2.74703930",
  "0.00535997": "1.13950885",
  "0.00536000": "53.12723251",
  "0.00537968": "5.27397661",
  "0.00539234": "3.63687246"
};

function getNObjects(dataset, depth) {
  var i = 0;
  for (var key in dataset) {
    if (i >= depth) {
      delete dataset[key];
    }
    i++;
  }
  return dataset;
}


console.log(getNObjects(dataset, 5));