如何通过操纵特定键

时间:2017-11-30 09:11:46

标签: javascript arrays json loops hashmap

尝试几天来操纵一些JSON数据响应。如何从以下创建哈希映射?

var jsonObject = JSON.parse(xhr.responseText); //response that parced

搜索"volvo, opel, honda。在解析之后,它会返回每个用户索引并看起来像这样(每个不同的搜索中jsonObject.length更改):

[{user: 1, car: volvo, score:7, time: "some time"},{user: 2, car: volvo, score:8, time: "some time"},{user: 3, car: volvo, score:9, time: "some time"},{user: 3, car: opel, score:6, time: "some time"},{user: 3, car: honda, score:8, time: "some time"},{user: 4, car: volvo, score:6, time: "some time"},{user: 4, car: opel, score:7, time: "some time"},{user: 5, car: honda, score:8, time: "some time"}]

我希望输出如下:

[{1:[7]},{2:[8]},{3:[9,6,8]},{4:[6,7]},{5:[8]}]

3 个答案:

答案 0 :(得分:1)

您可以使用对象缩小数组并收集每个用户的所有分数,以确保您将唯一的用户ID作为键



const jsonObject = [
  {user: 1, car: 'volvo', score:7, time: "some time"}
, {user: 2, car: 'volvo', score:8, time: "some time"}
, {user: 3, car: 'volvo', score:9, time: "some time"}
, {user: 3, car: 'opel', score:6, time: "some time"}
, {user: 3, car: 'honda', score:8, time: "some time"}
, {user: 4, car: 'volvo', score:6, time: "some time"}
, {user: 4, car: 'opel', score:7, time: "some time"}
, {user: 5, car: 'honda', score:8, time: "some time"} 
]

const results = jsonObject.reduce((map, obj) => {
  // set a new key based on the user id if it doesn't exist
  if (!map.has(obj.user)) {
    map.set(obj.user, [])
  }
  // add to the users scores to the users score
  map.get(obj.user).push(obj.score)
  // return the accumulator
  return map
}, new Map)

for (let [key, value] of results.entries()) {
  console.log({
    key,
    value
  })
}

<script src="https://codepen.io/synthet1c/pen/KyQQmL.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

使用ES5

Reduce数组,并使用辅助对象按用户名设置对创建对象的引用:

var data = [{user: 1, car: 'volvo', score:7, time: "some time"},{user: 2, car: 'volvo', score:8, time: "some time"},{user: 3, car: 'volvo', score:9, time: "some time"},{user: 3, car: 'opel', score:6, time: "some time"},{user: 3, car: 'honda', score:8, time: "some time"},{user: 4, car: 'volvo', score:6, time: "some time"},{user: 4, car: 'opel', score:7, time: "some time"},{user: 5, car: 'honda', score:8, time: "some time"}];

var helper = {};
var result = data.reduce(function(r, o) {    
  if(!helper[o.user]) {
    helper[o.user] = {};
    r.push(helper[o.user]);
    helper[o.user][o.user] = [];
  }
  
  helper[o.user][o.user].push(o.score);
  
  return r;
}, []);

console.log(result);

使用ES6

Reduce数组变为Map,然后spread地图的values返回数组:

const data = [{user: 1, car: 'volvo', score:7, time: "some time"},{user: 2, car: 'volvo', score:8, time: "some time"},{user: 3, car: 'volvo', score:9, time: "some time"},{user: 3, car: 'opel', score:6, time: "some time"},{user: 3, car: 'honda', score:8, time: "some time"},{user: 4, car: 'volvo', score:6, time: "some time"},{user: 4, car: 'opel', score:7, time: "some time"},{user: 5, car: 'honda', score:8, time: "some time"}];

const result = [...data.reduce((m, { user, score }) => {  
  const scores = m.get(user) || { [user]: [] };
  
  scores[user].push(score);
  
  return m.set(user, scores);
}, new Map()).values()];

console.log(result);

答案 2 :(得分:0)

这是一个简单的旧JS方法,没有map,reduce或stuffs

max_samples