不使用if语句将数据推送到数组

时间:2018-06-14 05:29:18

标签: javascript arrays

我有以下输出。它提供了我的API。

{
    "_id": {
        "year": 2018,
        "month": 6,
        "day": 11,
        "hour": 12,
        "interval": 45,
        "method": "200"
    },
    "count": 1
},
{
    "_id": {
        "year": 2016,
        "month": 11,
        "day": 11,
        "hour": 16,
        "interval": 50,
        "method": "404"
    },
    "count": 5
},
{
    "_id": {
        "year": 2016,
        "month": 11,
        "day": 11,
        "hour": 17,
        "interval": 10,
        "method": "200"
    },
    "count": 47
}}

我想根据方法将它们推送到数组。作为一个例子

twoHundArray=[
{ "x":2018,6,11,12,45,
  "y" :1},
{"x": 2016,11,11,17,10 ,
"y" :47}]

fourhundrArry=[{ "x":2018,11,11,16,50,
  "y" :5}]

不使用if / else语句如何将它们推送到不同的数组。在这里我不知道所有的方法名称。所以不能使用if语句为“方法”。这就是问题所在。

4 个答案:

答案 0 :(得分:1)

原始对象无效。如果没有指定键,则无法在对象中包含元素。我以为它是一个数组。

其次,在不知道名称的情况下,无法将元素推送到不同的数组。因此,将元素推送到不同变量的判断必须基于if/else条件。此外,这些变量的创建将因组而异,因为method可能是任何值。

如果您同意根据method的值对对象进行分组,请执行以下操作:



const data = [{"_id":{"year":2018,"month":6,"day":11,"hour":12,"interval":45,"method":"200"},"count":1},{"_id":{"year":2016,"month":11,"day":11,"hour":16,"interval":50,"method":"404"},"count":5},{"_id":{"year":2016,"month":11,"day":11,"hour":17,"interval":10,"method":"200"},"count":47}];
const res = {};

data.forEach(item => {
  const { method, ...obj } = item['_id'];
  res[method] = res[method] || [];

  res[method].push({
    x: Object.values(obj),
    y: item.count
  });
});

console.log(res);




它创建一个对象,其键为method。对象中的值是数组,其中包含按method分组的项目。

答案 1 :(得分:0)

您可以使用Array.reduce并根据方法创建地图。请尝试以下方法:

var data = [{
    "_id": {
        "year": 2018,
        "month": 6,
        "day": 11,
        "hour": 12,
        "interval": 45,
        "method": "200"
    },
    "count": 1
},
{
    "_id": {
        "year": 2016,
        "month": 11,
        "day": 11,
        "hour": 16,
        "interval": 50,
        "method": "404"
    },
    "count": 5
},
{
    "_id": {
        "year": 2016,
        "month": 11,
        "day": 11,
        "hour": 17,
        "interval": 10,
        "method": "200"
    },
    "count": 47
}];

var method = data.reduce((a,o)=>{
    if(!a[o._id.method]){
      a[o._id.method] = [];
    };
    var { method, ...ob } = o._id;
    a[o._id.method].push({
      "x": Object.values(ob).join(","),
      "y" : o.count
    });
     return a;
}, {});
console.log(method);

答案 2 :(得分:0)

您可以使用Array.reduce使用status:values键/对创建对象,并使用Object destructuring和默认分配发布,创建自变量。

const arr = [{"_id":{"year":2018,"month":6,"day":11,"hour":12,"interval":45,"method":"200"},"count":1},{"_id":{"year":2016,"month":11,"day":11,"hour":16,"interval":50,"method":"404"},"count":5},{"_id":{"year":2016,"month":11,"day":11,"hour":17,"interval":10,"method":"200"},"count":47}];

let obj = arr.reduce((a,c) => {
  a[c._id.method] = a[c._id.method] || [];
  a[c._id.method].push({"x" : Object.values(c._id).join(),  "y" : c.count});
  return a;
},{});

/* You can add an entry here for every status type, it will pick the 
** value from object and if not present will be defaulted to an empty array */
const {200 : twoHundArray=[], 404 : fourHundArray=[], 300 : threeHundArray=[]} = obj;
console.log(twoHundArray);
console.log(fourHundArray);
console.log(threeHundArray);

答案 3 :(得分:0)

@Palani ,我建议你使用一个对象来收集所有必需的信息。

如果您需要,请查看以下代码并告诉我任何建议/修改。

H:\RishikeshAgrawani\Projects\Sof\FilterArrays>node FilterArrays.js
{
    "200Array": [
        {
            "x": "2018, 6, 11, 12, 45",
            "y": 1
        },
        {
            "x": "2016, 11, 11, 17, 10",
            "y": 47
        }
    ],
    "404Array": [
        {
            "x": "2016, 11, 11, 16, 50",
            "y": 5
        }
    ]
}
[
    {
        "x": "2018, 6, 11, 12, 45",
        "y": 1
    },
    {
        "x": "2016, 11, 11, 17, 10",
        "y": 47
    }
]
[
    {
        "x": "2016, 11, 11, 16, 50",
        "y": 5
    }
]

输出»

{{1}}