使用地图功能对对象进行迭代和分组

时间:2018-12-26 02:40:34

标签: javascript arrays json loops

检查十进制ID并进行相应分组。

下面是示例和推荐的JSON

示例JSON

{
    "results": [
        {
            "name": "Download",
            "id": "1.1.1"
        },
        {
            "name": "Download",
            "id": "1.2"
        },
        {
            "name": "Download",
            "id": "1.3.2"
        },
        {
            "name": "Download",
            "id": "2"
        },
        {
            "name": "Download",
            "id": "2.3"
        },
        {
            "name": "Download",
            "id": "3.2"
        },
        {
            "name": "Download",
            "id": "3.5"
        },
        {
            "name": "Download",
            "id": "4.2"
        }
    ]
}

想要将上述JSON迭代并重新构造为以下推荐格式。

逻辑:应检查ID(带小数和不带小数),并根据数字对它们进行分组。

例如:

1, 1.1, 1.2.3, 1.4.5 => data1: [{id: 1},{id: 1.1}....] 
2, 2.3, 2.3.4 => data2: [{id: 2},{id: 2.3}....]
3, 3.1 => data3: [{id: 3},{id: 3.1}]

推荐的JSON

{
    "results": [
        {
            "data1": [
                {
                    "name": "Download",
                    "id": "1.1.1"
                },
                {
                    "name": "Download",
                    "id": "1.2"
                },
                {
                    "name": "Download",
                    "id": "1.3.2"
                }
            ]
        },
        {
            "data2": [
                {
                    "name": "Download",
                    "id": "2"
                },
                {
                    "name": "Download",
                    "id": "2.3"
                }
            ]
        },
        {
            "data3": [
                {
                    "name": "Download",
                    "id": "3.2"
                },
                {
                    "name": "Download",
                    "id": "3.5"
                }
            ]
        },
        {
            "data4": [
                {
                    "name": "Download",
                    "id": "4.2"
                }
            ]
        }
    ]
}

我尝试了以下解决方案,但未将对象分组

var formatedJSON = [];
results.map(function(d,i) {
    formatedJSON.push({
        [data+i]: d
    })
});

谢谢。

5 个答案:

答案 0 :(得分:2)

您可以像这样使用reduce。这个想法是为每个data1data2等创建一个键-值对,以便该对象中的值是您在最终数组中所需的值。然后使用Object.values将它们作为数组获取。

const sampleJson = {"results":[{"name":"Download","id":"1.1.1"},{"name":"Download","id":"1.2"},{"name":"Download","id":"1.3.2"},{"name":"Download","id":"2"},{"name":"Download","id":"2.3"},{"name":"Download","id":"3.2"},{"name":"Download","id":"3.5"},{"name":"Download","id":"4.2"}]}

const grouped = sampleJson.results.reduce((a, v) => {
    const key = `data${parseInt(v.id)}`;
    (a[key] = a[key] || {[key]: []})[key].push(v);
    return a;
},{});

console.log({results: Object.values(grouped)})

一个衬板/代码高尔夫球:

let s={"results":[{"name":"Download","id":"1.1.1"},{"name":"Download","id":"1.2"},{"name":"Download","id":"1.3.2"},{"name":"Download","id":"2"},{"name":"Download","id":"2.3"},{"name":"Download","id":"3.2"},{"name":"Download","id":"3.5"},{"name":"Download","id":"4.2"}]},k;

console.log({results:Object.values(s.results.reduce((a,v)=>(k=`data${parseInt(v.id)}`,(a[k] = a[k]||{[k]:[]})[k].push(v),a),{}))})

答案 1 :(得分:0)

您在这里:

var data = {
    "results": [
        {
            "name": "Download",
            "id": "1.1.1"
        },
        {
            "name": "Download",
            "id": "1.2"
        },
        {
            "name": "Download",
            "id": "1.3.2"
        },
        {
            "name": "Download",
            "id": "2"
        },
        {
            "name": "Download",
            "id": "2.3"
        },
        {
            "name": "Download",
            "id": "3.2"
        },
        {
            "name": "Download",
            "id": "3.5"
        },
        {
            "name": "Download",
            "id": "4.2"
        }
    ]
};


let newSet = new Set();

data.results.forEach(e => {
  let key = e.id.substring(0, e.id.indexOf('.'));
  console.log(key);
  if (newSet.has(key) == false) {
    newSet.add(key);
    newSet[key] = [];
    }
   newSet[key].push(e.id);
});


console.log(newSet);

答案 2 :(得分:0)

这是您的操作方式:

var data = {
    "results": [
        {
            "name": "Download",
            "id": "1.1.1"
        },
        {
            "name": "Download",
            "id": "1.2"
        },
        {
            "name": "Download",
            "id": "1.3.2"
        },
        {
            "name": "Download",
            "id": "2"
        },
        {
            "name": "Download",
            "id": "2.3"
        },
        {
            "name": "Download",
            "id": "3.2"
        },
        {
            "name": "Download",
            "id": "3.5"
        },
        {
            "name": "Download",
            "id": "4.2"
        }
    ]
};

var newData = {
  "results": {}
};

data.results.forEach(item => {
  var num = item.id.slice(0, 1);
  if (newData.results["data" + num]) {
    newData.results["data" + num].push(item);
  } else {
    newData.results["data" + num] = [item];
  }
})

data = newData;
console.log(data);

它的作用是遍历results中的每个项目,获取该项目id前面的数字,并检查名称数组data-{num}是否存在。如果数组存在,则将其推送。如果它不存在,则使用项目创建。

答案 3 :(得分:0)

let input = getInput();

let output = input.reduce((acc, curr)=>{
  let {id} = curr;
  let majorVersion = 'name' + id.split('.')[0];
  if(!acc[majorVersion]) acc[majorVersion]= [];
  acc[majorVersion].push(curr);
  return acc;
},{})
  
console.log(output)
  
  
  
function getInput(){
  return [
  {
      "name": "Download",
      "id": "1.1.1"
  },
  {
      "name": "Download",
      "id": "1.2"
  },
  {
      "name": "Download",
      "id": "1.3.2"
  },
  {
      "name": "Download",
      "id": "2"
  },
  {
      "name": "Download",
      "id": "2.3"
  },
  {
      "name": "Download",
      "id": "3.2"
  },
  {
      "name": "Download",
      "id": "3.5"
  },
  {
      "name": "Download",
      "id": "4.2"
  }

  ]
  }

答案 4 :(得分:0)

使用RegEx进行精确控制的一种解决方案,因为它可以轻松区分1和11。 而且,这还将确保即使以相同的版本结尾(例如结尾为1.9),也可以将其放回data1中。

let newArr2 = ({ results }) =>
  results.reduce((acc, item) => {
    let key = "data" + /^(\d+)\.?.*/.exec(item.id)[1];
    let found = acc.find(i => key in i);
    found ? found[key].push(item) : acc.push({ [key]: [item] });
    return acc;
  }, []);