如何使用Object.keys()?

时间:2017-06-16 23:44:33

标签: arrays json for-loop foreach javascript-objects

我通常习惯使用Object.keys()函数。 但是这一次,我无法通过名称读取属性的值,对于以下JSON中的每个对象:

JSON

var myData = {
  "customNotes":{
    "2017/04/17":{
      "concernedDate":"April, 17, 2017",
      "notesList":[
        {
          "id":25,
          "title":"Note 25 Title"
        },
        {
          "id":51,
          "title":"Note 51 Title"
        }
      ]
    },
    "2017/04/14":{
      "concernedDate":"April, 14, 2017",
      "notesList":[
        {
          "id":53,
          "title":"Note 53 Title"
        }
      ]
    }
  }
}

我需要的输出

我需要的是以下输出:

    2017/04/17
    concernedDate: April, 17, 2017
    Number of notes: 2
    Notes list:
    - id: 25
    - title: Note 25 Title
    - id: 51
    - title: Note 51 Title
    - - - - - - - -
    2017/04/14
    concernedDate: April, 14, 2017
    Number of notes: 1
    Notes list:
    - id: 53
    - title: Note 53 Title

我的错误JS代码

$(Object.keys(myData.customNotes)).each(function(iGroup){

    //Get Key/Val of each Group of notes
    var keyGroup = Object.keys(myData.customNotes)[iGroup];
    var valGroup = myData.customNotes[keyGroup];

    //Print Key (the date as a string)
    console.log(valGroup[0]);

    //Print Key Property ('concernedDate')
    console.log('concernedDate: ' + valGroup[0].concernedDate);

    //Print Key Property Length ('notesList')
    console.log('Number of notes: ' + valGroup[0].notesList.length);

    //Print List of notes
    console.log('Notes list:');

    //If the property exists
    if(valGroup[0].notesList){
        //For each item in 'notesList'
        $(Object.keys(valGroup[0].notesList)).each(function(iNote){

            //Get Key/Val For each note
            var keyNote = Object.keys(valGroup[0].notesList)[iNote];
            var valNote = valGroup[0].notesList[keyNote];

            //Print the properties of each note
            console.log('- id: ' + valNote.id);
            console.log('- title: ' + valNote.title);
        });
    }

});

3 个答案:

答案 0 :(得分:1)

es6

    .DisplayBlock {
      display: block;
    }

    .DisplayNone {
      display: none
    }

    a {
        display: block;
        margin: 30px;
        padding: 40px;
        background: seagreen;
        color: palegreen;
        font-family: verdana, helvetica, arial, tahoma;
    }

<强> ES5

    
    <div>
        <a style="cursor:pointer" onclick="showhide()">Show/Hide</a>
        <a id="mobile_index" class="DisplayNone">P&aacute;gina Inicial</a>
    </div>

答案 1 :(得分:0)

首先,我要感谢@cheesenthusiast的回答:它就像一个魅力,你绝对应该依赖它,无论是ES6还是ES5!

与此同时,我自己进行了实验,并以更加“经典的循环式”方式找到了另一个有效的解决方案 相同的结果

另一种工作方法(更经典)

所以对于那些希望在脚本的每一步都有更多控制权的人来说,这是另一种工作方式

//WORKING
console.log('---START LIST---');

//For each GROUP OF NOTES
for (var item in myData.customNotes) {
    //Print GROUP OF NOTES key
    console.log(item);

    //Print GROUP OF NOTES properties
    console.log('concernedDate: ' + myData.customNotes[item].concernedDate);
    console.log('Number of notes: ' + myData.customNotes[item].notesList.length);
    console.log('Notes list: ');

    //For each NOTE
    $(Object.keys(myData.customNotes[item].notesList)).each(function(iGroup){

        //Get this Array item
        var keyGroup = Object.keys(myData.customNotes[item].notesList)[iGroup];
        var valGroup = myData.customNotes[item].notesList[keyGroup];

        //Print NOTE properties
        console.log('- id: ' + valGroup.id);
        console.log('- title: ' + valGroup.title);

    });
    console.log('- - - - - -');

}
console.log('---END LIST---');

答案 2 :(得分:0)

分拣

在我的对象中添加了一些元素后,我需要按 KEY (日期:示例2017/04/17)对其进行排序。

但无论我尝试什么,它总是以相同的顺序返回相同的项目:

//Creating an Array of SORTED values (Nothing works :-/)
var tmpArr = Object.keys(myData.customNotes).sort((a, b) => a > b ? a : b);
var tmpArr = Object.keys(myData.customNotes).sort((a, b) => Date(a).valueOf() > Date(b).valueOf() ? a : b);
var tmpArr = Object.keys(myData.customNotes).sort((a, b) => a.valueOf() > b.valueOf() ? a : b);
var tmpArr = Object.keys(myData.customNotes).sort((a, b) => a-b);


//Convert back the Array to an Object
var myData.customNotes = tmpArr.reduce(function(acc, cur, i) {
  acc[i] = cur;
  return acc;
}, {});