Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.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 如何将JSON对象移动到[X]组中_Javascript_Json - Fatal编程技术网

Javascript 如何将JSON对象移动到[X]组中

Javascript 如何将JSON对象移动到[X]组中,javascript,json,Javascript,Json,我有一组JSON对象,我正试图将它们分组为X组。看起来像这样: { "1": { "id": "XXXXXXX", "author": "1001", }, "2": { "id": "XXXXXXX", "author": "1001", }, "3": { "id": "XXXXXXX", "author": "1001", }, "4": { "id": "XXXXXXX", "a

我有一组JSON对象,我正试图将它们分组为X组。看起来像这样:

{
"1": {
     "id": "XXXXXXX",
     "author": "1001",
     },
"2": {
     "id": "XXXXXXX",
     "author": "1001",
     },
"3": {
     "id": "XXXXXXX",
     "author": "1001",
     },
"4": {
     "id": "XXXXXXX",
     "author": "998",
     }
}
{
    "998": {
        "4": {
            "id": "XXXXXXX",
            "author": "998"
        }
    },
    "1001": {
        "1": {
            "id": "XXXXXXX",
            "author": "1001"
        },
        "2": {
            "id": "XXXXXXX",
            "author": "1001"
        },
        "3": {
            "id": "XXXXXXX",
            "author": "1001"
        }
    }
}
(ID都是不同的值) 假设我想获取author值是否为1001,我已经这样做了:

var userid=“1001”;
对于(modfile中的var v){//modfile是上面的JSON对象
if(对象的modfile实例){
如果(modfile[v].author!==userid){continue;}
}}继续;
现在我需要知道我不能做什么。我想抓取这些对象,例如对象1、2和3,然后将其移动到X组中

{
"group1":{
"1":{
"id":"XXXXXX",
"author":"1001"
},
"2":{
"id":"XXXXXX",
"author":"1001"
}
}
"group2":{
"3":{
"id":"XXXXXX",
"author":"1001"
}
}

让我们假设2人一组。我该怎么做?

这里有一种方法:

var group = {};
Object.keys(a).forEach(function(k) { // i.e. for "1", "2", "3"...
    groups[a[k].author] = groups[a[k].author] || {}; // create an empty object with key as author id
    groups[a[k].author][k] = a[k]; // add the object to the group
});
这将按作者ID对对象进行分组,如下所示:

{
"1": {
     "id": "XXXXXXX",
     "author": "1001",
     },
"2": {
     "id": "XXXXXXX",
     "author": "1001",
     },
"3": {
     "id": "XXXXXXX",
     "author": "1001",
     },
"4": {
     "id": "XXXXXXX",
     "author": "998",
     }
}
{
    "998": {
        "4": {
            "id": "XXXXXXX",
            "author": "998"
        }
    },
    "1001": {
        "1": {
            "id": "XXXXXXX",
            "author": "1001"
        },
        "2": {
            "id": "XXXXXXX",
            "author": "1001"
        },
        "3": {
            "id": "XXXXXXX",
            "author": "1001"
        }
    }
}

我相信它可以进一步优化。

这是一个很好的练习,但在实践中,您应该尽可能在编号对象上使用数组。效率要高得多

在任何情况下,下面的实现都可以通过可变组大小满足您的需求,如果没有第二个参数,则默认为每个组两个

它创建一个新对象,使用两个计数器跟踪正在生成的组和当前组中的项数。它推入新对象的当前组命名空间,直到组中的项数等于groupSize。然后,它将创建一个新的组名称空间,增加组计数器,并将组项计数器重置为零

const groupJsonParser = (json, groupSize=2) => {
  let counter = 0, // tracks new new property in the object
    groups = 0,  // tracks the groups created
    groupKey = `group${++groups}` // key to use for the group
  // iteration through the object is required. Easiest 
  // way to do this is to get the keys as an array and 
  // iterate through them. Here I'll directly synthesize the new
  // object using the reduce array method: 
  return Object.keys(json).reduce((final, key) => {
    // check if the number pushed into the current group 
    // exceeds the group limit: 
    if(counter === groupSize) {
      // if it does, reset the counter to 0
      counter = 0
      // and update the group key for the new group
      groupKey = `group${++groups}`
      // create the object for the new group and assign to
      // the new object with the group key: 
      final[groupKey] = {}
    }
    // in the current group at the current counter, set
    // the current value from the original json
    // and increment the counter: 
    final[groupKey][++counter] = json[key]

    // always return the updated object at the bottom 
    // of every iteration of reduce:
    return final
  }, {[groupKey]: {}}) /*<-- initialization value*/
}
constgroupjsonparser=(json,groupSize=2)=>{
让计数器=0,//跟踪对象中的新属性
groups=0,//跟踪创建的组
groupKey=`group${++groups}`//用于组的键
//需要通过对象进行迭代。最简单
//实现这一点的方法是将键作为数组和
//在这里我将直接合成新的
//对象使用reduce数组方法:
返回Object.keys(json).reduce((final,key)=>{
//检查号码是否已推入当前组
//超出集团限额:
如果(计数器===组大小){
//如果是,请将计数器重置为0
计数器=0
//并更新新组的组密钥
groupKey=`group${++groups}`
//为新组创建对象并指定给
//具有组键的新对象:
final[groupKey]={}
}
//在当前计数器的当前组中,设置
//原始json中的当前值
//并递增计数器:
final[groupKey][++counter]=json[key]
//始终在底部返回更新的对象
//在reduce的每次迭代中:
返回决赛

},{[groupKey]:{}}/*您能更深入地解释一下要创建的修改对象吗?假设我每个组需要2个过滤对象,因此需要2个组,因为有3个对象(过滤),所以有组1,包含对象1和2。组2有对象3。编号的对象(我更新过)应该有id和作者。