简体   繁体   English

将对象键/值对拆分为单独的对象

[英]Split object key/value pairs into separate objects

I was wondering is there a way to split key/value pairs of objects and insert them into array so that I get array of objects. 我想知道是否有一种方法可以拆分对象的键/值对并将其插入数组,以便获得对象数组。

Here is the code: 这是代码:

let grades = [5,5,6,7,6,7,9,10,8,6]

let gradesSpread = {
   5:0,
   6:0,
   7:0,
   8:0,
   9:0,
   10:0
}


// This populates spreadGrades object as intended
for( let [index,grade] in grades.entries() ) {
   gradesSpread[grade]++
}

So now I want to get key/value pair from gradesSpread and create object for each and push it into new array. 所以现在我想从gradesSpread获取键/值对,并为每个对象创建对象并将其推入新数组。

I know I can use Object.values() and Object.keys() to get keys or values from object but I have no clue how I would go about adding those key value pairs into array of objects 我知道我可以使用Object.values()Object.keys()从对象中获取键或值,但是我不知道如何将这些键值对添加到对象数组中

For better visualization I need array to look something like this : 为了更好的可视化,我需要数组看起来像这样:

let finalSpread = [{5:0},{6:4}...]

I am open to all solutions also bonus points for making it so it does not need initialized object. 我对所有解决方案都持开放态度,也为此获得了加分,因此不需要初始化的对象。

var arr = [];
Object.keys(grades).forEach((key) => {
    var newObj = {};
    newObj[key] = grades[key];
    arr.push(newObj);
});

Using ES6 it could be shorter: 使用ES6可能会更短:

var arr = [];
Object.keys(grades).forEach((key) => {
    arr.push({[key]: grades[key});
});

You could map the keys with the values. 您可以将键与值映射。 The order does not reflect the order of insertation of the object. 该顺序不反映对象插入的顺序。

 var object = { 5: 1, 6: 3, 2: 4 }, result = Object .keys(object) .map(k => ({ [k]: object[k] })); console.log(result); 

Sounds like you want Array.prototype.reduce() . 听起来像您想要Array.prototype.reduce()

let grades = [5,5,6,7,6,7,9,10,8,6];

let finalSpread = grades.reduce((spread, grade) => {
  let count = spread[grade] || 0;
  spread[grade] = ++count;
  return spread;
}, {});

Seeing as you want a final value that's essentially a map of key-values, I'd recommend finalSpread being an object like above. 看到您想要一个最终值,该值本质上是键值映射,我建议finalSpread是上面的对象。



Update: 更新:

If it's imperative that it be an array for whatever reason you could: 如果无论出于何种原因都必须将其作为数组,则可以:

a. 一种。 Add finalSpread = Object.entries(finalSpread) at the end (note: each grade will actually be a string) 最后添加finalSpread = Object.entries(finalSpread) (注意:每个年级实际上都是一个字符串)

b. b。 Just refactor the function in reduce to check if they array of [grade, count] already exists and either add it or increment it. 只需在reduce中重构函数,以检查它们是否已存在[grade,count]数组,然后添加或增加它。

c. C。 use a Map instead of an array as the reduce accumulator (if you need the grades to also be numbers - Map allows non-string keys) - then cast it to an array, ie: 使用Map而不是数组作为reduce累加器(如果您还需要等级也为数字-Map允许使用非字符串键)-然后将其强制转换为数组,即:

let finalSpread = grades.reduce((spread, grade) => {
  let count = spread.get(grade) || 0;
  spread.set(grade, ++count);
  return spread;
}, new Map());
finalSpread = Array.from(finalSpread);

(note: map and Array.from() have limited browser support.) (注意: mapArray.from()具有有限的浏览器支持。)

You may also try: 您也可以尝试:

let arrayOfObj = [];
let extractPairs = (jsonObj) => {
for(let item in jsonObj) {
    console.log(item);
    arrayOfObj.push({item: jsonObj[item]})
}
console.log(arrayOfObj);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM