Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 按日期对数组中的项进行分组_Javascript_Node.js - Fatal编程技术网

Javascript 按日期对数组中的项进行分组

Javascript 按日期对数组中的项进行分组,javascript,node.js,Javascript,Node.js,我有一个包含日期和bin颜色的数组。一些垃圾箱是在同一天收集的,所以我想对这些日期进行分组,然后设置一个垃圾箱颜色数组 我希望实现的数据结构是: "items": [ "item": { "date": "2019-10-18T00:00:00.000Z", "bins": [{ "bin": "Blue" }, { "bin": "Grey"

我有一个包含日期和bin颜色的数组。一些垃圾箱是在同一天收集的,所以我想对这些日期进行分组,然后设置一个垃圾箱颜色数组

我希望实现的数据结构是:

"items": [
        "item": {
            "date": "2019-10-18T00:00:00.000Z",
            "bins": [{
                "bin": "Blue"
            }, {
                "bin": "Grey"
            }]
        },
        ...
]
数据阵列:

var dateList = [{
        "date": "2019-10-18T00:00:00.000Z",
        "bin": "Blue"
    }, {
        "date": "2019-11-08T00:00:00.000Z",
        "bin": "Blue"
    }, {
        "date": "2019-11-01T00:00:00.000Z",
        "bin": "Green"
    }, {
        "date": "2019-11-22T00:00:00.000Z",
        "bin": "Green"
    }, {
        "date": "2019-10-18T00:00:00.000Z",
        "bin": "Grey"
    }, {
        "date": "2019-11-01T00:00:00.000Z",
        "bin": "Grey"
    }]
这是我到目前为止的代码,它几乎就在那里,但是没有创建items.item,而是创建items[date],但我无法理解最后一部分:

    var items = {};
    for(var i = 0; i < dateList.length; i++) {

        if(!items[dateList[i].date]) {

            items[dateList[i].date] = {};
            items[dateList[i].date].date = dateList[i].date;
            items[dateList[i].date].bins = [];
            items[dateList[i].date].bins.push( { "bin": dateList[i].bin });

        } else {
            items[dateList[i].date].bins.push( { "bin": dateList[i].bin }); 
        }
    } 
如果我能像items那样组织它,我会更喜欢它

"items": [
    "item": {
        "date": "2019-10-18T00:00:00.000Z",
        "bins": [{
            "bin": "Blue"
        }, {
            "bin": "Grey"
        }]
    },
    "item": {
        "date": "2019-11-08T00:00:00.000Z",
        "bins": [{
            "bin": "Blue"
        }]
    },
    "item": {
        "date": "2019-11-01T00:00:00.000Z",
        "bins": [{
            "bin": "Green"
        }, {
            "bin": "Grey"
        }]
    },
    "item": {
        "date": "2019-11-22T00:00:00.000Z",
        "bins": [{
            "bin": "Green"
        }]
    }
]

感谢

获取对象数组,您可以将其用作以下代码。请注意,使用
Object.keys()
时的顺序不受保证

我放了两种方法,一种是简单数组,第二种是数组,数组中的每个对象都有
item
关键字作为包装器

const项={
“2019-10-18T00:00:00.000Z”:{
“日期”:“2019-10-18T00:00:00.000Z”,
“垃圾箱”:[{
“bin”:“蓝色”
}, {
“bin”:“灰色”
}]
},
“2019-11-08T00:00:00.000Z”:{
“日期”:“2019-11-08T00:00:00.000Z”,
“垃圾箱”:[{
“bin”:“蓝色”
}]
},
“2019-11-01T00:00:00.000Z”:{
“日期”:“2019-11-01T00:00:00.000Z”,
“垃圾箱”:[{
“bin”:“绿色”
}, {
“bin”:“灰色”
}]
},
“2019-11-22T00:00:00.000Z”:{
“日期”:“2019-11-22T00:00:00.000Z”,
“垃圾箱”:[{
“bin”:“绿色”
}]
}
}
常量数组=Object.keys(items).map(key=>items[key]);
const arraywhitems=Object.keys(items).map(key=>({item:items[key]}));
console.log(数组);
console.log(“!!------------!!!!!!!”);

console.log(arraywhitems)要获取对象数组,可以将其用作以下代码。请注意,使用
Object.keys()
时的顺序不受保证

我放了两种方法,一种是简单数组,第二种是数组,数组中的每个对象都有
item
关键字作为包装器

const项={
“2019-10-18T00:00:00.000Z”:{
“日期”:“2019-10-18T00:00:00.000Z”,
“垃圾箱”:[{
“bin”:“蓝色”
}, {
“bin”:“灰色”
}]
},
“2019-11-08T00:00:00.000Z”:{
“日期”:“2019-11-08T00:00:00.000Z”,
“垃圾箱”:[{
“bin”:“蓝色”
}]
},
“2019-11-01T00:00:00.000Z”:{
“日期”:“2019-11-01T00:00:00.000Z”,
“垃圾箱”:[{
“bin”:“绿色”
}, {
“bin”:“灰色”
}]
},
“2019-11-22T00:00:00.000Z”:{
“日期”:“2019-11-22T00:00:00.000Z”,
“垃圾箱”:[{
“bin”:“绿色”
}]
}
}
常量数组=Object.keys(items).map(key=>items[key]);
const arraywhitems=Object.keys(items).map(key=>({item:items[key]}));
console.log(数组);
console.log(“!!------------!!!!!!!”);
console.log(arraywhitems)我对此的看法:

var日期列表=[{
“日期”:“2019-10-18T00:00:00.000Z”,
“bin”:“蓝色”
}, {
“日期”:“2019-11-08T00:00:00.000Z”,
“bin”:“蓝色”
}, {
“日期”:“2019-11-01T00:00:00.000Z”,
“bin”:“绿色”
}, {
“日期”:“2019-11-22T00:00:00.000Z”,
“bin”:“绿色”
}, {
“日期”:“2019-10-18T00:00:00.000Z”,
“bin”:“灰色”
}, {
“日期”:“2019-11-01T00:00:00.000Z”,
“bin”:“灰色”
}]
让dictionary={};
for(设i=0;iobj.bins=新数组(…新集合(obj.bins));
控制台日志(结果)我对此的看法:

var日期列表=[{
“日期”:“2019-10-18T00:00:00.000Z”,
“bin”:“蓝色”
}, {
“日期”:“2019-11-08T00:00:00.000Z”,
“bin”:“蓝色”
}, {
“日期”:“2019-11-01T00:00:00.000Z”,
“bin”:“绿色”
}, {
“日期”:“2019-11-22T00:00:00.000Z”,
“bin”:“绿色”
}, {
“日期”:“2019-10-18T00:00:00.000Z”,
“bin”:“灰色”
}, {
“日期”:“2019-11-01T00:00:00.000Z”,
“bin”:“灰色”
}]
让dictionary={};
for(设i=0;iobj.bins=新数组(…新集合(obj.bins));

控制台日志(结果)首先,您期望的结果无效。javascript数组将不接受键值。数组是项或对象的列表。下面是您可以获得的输出

//Output
[
  {
    "date": "2019-10-18T00:00:00.000Z",
    "bins": [
      {
        "bin": "Blue"
      },
      {
        "bin": "Grey"
      }
    ]
  },
  {
    "date": "2019-11-08T00:00:00.000Z",
    "bins": [
      {
        "bin": "Blue"
      }
    ]
  },
  {
    "date": "2019-11-01T00:00:00.000Z",
    "bins": [
      {
        "bin": "Green"
      },
      {
        "bin": "Grey"
      }
    ]
  },
  {
    "date": "2019-11-22T00:00:00.000Z",
    "bins": [
      {
        "bin": "Green"
      }
    ]
  }
]
下面是派生输出的javascript代码

//Code
var noDuplicates = [];
var lookupObject  = {};
for(var i in dateList) {
    lookupObject[dateList[i]['date']] = dateList[i];
}

//Create the array with unique dates
for(i in lookupObject) {
    noDuplicates.push(lookupObject[i].date);
}

var results=[]
//Iterate and filter the original array with same date value
for(i in noDuplicates) {
    var groupedArray = dateList.filter(obj => {
      return obj.date === noDuplicates[i]
    })
    //Form the bins array for the output
    var bins = groupedArray.map(obj => {
      return {
        'bin': obj.bin
      }
    })
    //Construct the output object
    results.push({
      'date': groupedArray[0].date,
      'bins': bins
    })
}

对。这是一个简单的解决方案,只需稍加操作即可首先获得唯一的值。希望它能解决您的问题。

首先,您期望的结果无效。javascript数组将不接受键值。数组是项或对象的列表。下面是您可以获得的输出

//Output
[
  {
    "date": "2019-10-18T00:00:00.000Z",
    "bins": [
      {
        "bin": "Blue"
      },
      {
        "bin": "Grey"
      }
    ]
  },
  {
    "date": "2019-11-08T00:00:00.000Z",
    "bins": [
      {
        "bin": "Blue"
      }
    ]
  },
  {
    "date": "2019-11-01T00:00:00.000Z",
    "bins": [
      {
        "bin": "Green"
      },
      {
        "bin": "Grey"
      }
    ]
  },
  {
    "date": "2019-11-22T00:00:00.000Z",
    "bins": [
      {
        "bin": "Green"
      }
    ]
  }
]
下面是派生输出的javascript代码

//Code
var noDuplicates = [];
var lookupObject  = {};
for(var i in dateList) {
    lookupObject[dateList[i]['date']] = dateList[i];
}

//Create the array with unique dates
for(i in lookupObject) {
    noDuplicates.push(lookupObject[i].date);
}

var results=[]
//Iterate and filter the original array with same date value
for(i in noDuplicates) {
    var groupedArray = dateList.filter(obj => {
      return obj.date === noDuplicates[i]
    })
    //Form the bins array for the output
    var bins = groupedArray.map(obj => {
      return {
        'bin': obj.bin
      }
    })
    //Construct the output object
    results.push({
      'date': groupedArray[0].date,
      'bins': bins
    })
}

对。这是一个简单的解决方案,只需稍加操作即可首先获得唯一的值。希望它能解决您的问题。

您的需求无法满足:您需要一个对象
具有多个属性,且具有相同的
名称。属性名称必须是唯一的。编辑后,您有一个(?)键/值对的
数组?尝试使用JSON感知编辑器并正确格式化JSON结构。您似乎对JavaScript中对象的工作方式有一些误解。对象的每个成员都必须是唯一的,因此它们不能都有item的名称。@A.Chiesa我错了,我已经更新了对item数组的要求。@scott-即使在更新了无效数组之后…您的要求也是强制的