在字典列表中列出值

时间:2019-03-28 12:22:46

标签: javascript

var dicList = [{
  student_id: 334,
  full_name: "student B",
  score: 9,
  class_id: 222
}, {
  student_id: 333,
  full_name: "student A",
  score: 7,
  class_id: 222
}]

for (var i = 0; i++; i < dicList.length) {
  for (var key in dicList[i]) {
    if (test.hasOwnProperty(key)) {
      console.log(key, dicList[i][key]);
    }
  }
}

当前返回未定义,我希望它返回每个字典中每个属性的值列表

2 个答案:

答案 0 :(得分:2)

您可以使用flatMap()Object.entries()forEach()遍历结果数组。

Array.prototype.flatMap = function(f){
  return [].concat(...this.map(f))
}
var dicList = [{student_id: 334,  full_name: "student B",  score: 9,  class_id: 222}, {  student_id: 333,  full_name: "student A",  score: 7,  class_id: 222}]

const res = dicList.flatMap(Object.entries)
res.forEach(([key,value]) => console.log(`key:${key} value:${value}`));

flatMap()并非在所有浏览器中都有效,因此您使用map()并创建嵌套的forEach()

var dicList = [{student_id: 334,  full_name: "student B",  score: 9,  class_id: 222}, {  student_id: 333,  full_name: "student A",  score: 7,  class_id: 222}]

const res = dicList.map(Object.entries)
res.forEach(a => a.forEach(([key,value]) => console.log(`key:${key} value:${value}`)));
Array.prototype.flatMap = function(f){
  return [].concat(...this.map(f))
}

您还可以为flatMap()

创建polyfill
if(!Array.prototype.flatMap){
  Array.prototype.flatMap = function(f){
    return [].concat(...this.map(f))
  }
}

答案 1 :(得分:1)

您需要

  • for statement的最后两个部分 RequestContext context = RequestContext.getCurrentContext(); HttpServletRequest request = context.getRequest(); StringBuffer originalURL = request.getRequestURL(); // Returns original url // ...modifying the requestURL StringBuffer newURL = request.getRequestURL(); // Returns new url condition 部分夹在中间,并且

    final-expression
  • 检查for ([initialization]; [condition]; [final-expression]) statement 而不是dicList[1]

test