简体   繁体   中英

JavaScript traversing through arrays

I am trying javascript after 15 years, so not a pro at this. I finished the grouping & sorting on array. Need help with traversing & setting the values in the result.

PART 1 - input data array -

var arr = [
      { id: 1, name: 'fryh', name1: 'ksdtfuy', group: '1208', DT: '2019-10-02T19:20' },
      { id: 2, name: 'fthy', name1: 'xdhftdx', group: '1209',  DT: '2019-10-02T09:27' },
      { id: 3, name: 'qjkgjwer', name1: 'dhfdt', group: '1208',  DT: '2019-10-02T09:24' },
       { id: 4, name: 'jlkke', name1: 'atth', group: '1208', DT: '2019-10-02T19:20' },
      { id: 5, name: 'gfhjm', name1: 'dryt', group: '1208',  DT: '2019-10-02T09:28' },
      { id: 6, name: 'iuit', name1: 'xx', group: '1209',  DT: '2019-10-02T09:24' },
    ];

want to group this based on group then sort it based on DT. Achieved this using the below code & the result is in grouped.

function ReSeq(){
        var grouped = {};
        for (var i = 0; i < arr.length; i += 1) {
            if(!grouped[arr[i].group]) {
            grouped[arr[i].group] = [];
          }
          grouped[arr[i].group].push(arr[i]);
        }
        for (var group in grouped) {
          grouped[group] = grouped[group].sort(sortByPosition);
        }       
        console.log(grouped);               
    }
var sortByPosition = function(a, b) {
  var dateA = new Date(a.position), dateB = new Date(b.position);
  return dateA - dateB;
};

Below is what is printed on console - Question is how do I traverse grouped & set values. Example i want to set values of name1 colum in grouped. basically i want to give it sequence so 1st entry in name 1 would be 1 the 2nd one would be 2 ...So the Need is to traverse the output object & set values in name1

{1208: Array(4), 1209: Array(2)}
1208: Array(4)
0: {id: 3, name: "qwer", name1: "xx", group: "1208", position: "2019-10-02T09:24"}
1: {id: 5, name: "qwe", name1: "xx", group: "1208", position: "2019-10-02T09:28"}
2: {id: 1, name: "qw", name1: "xx", group: "1208", position: "2019-10-02T19:20"}
3: {id: 4, name: "qw", name1: "xx", group: "1208", position: "2019-10-02T19:20"}
length: 4
__proto__: Array(0)
1209: Array(2)
0: {id: 6, name: "qwer", name1: "xx", group: "1209", position: "2019-10-02T09:24"}
1: {id: 2, name: "qwe", name1: "xx", group: "1209", position: "2019-10-02T09:27"}
length: 2
__proto__: Array(0)
__proto__: Object

The are several ways to do that. Here, Array.forEach (see MDN ) is used to revalue .name1 and for (let x in [obj]) to iterate through the object entries:

 // use Array.reduce to group const sortedAscendingAndGrouped = getArr() .sort((a, b) => new Date(a.DT) - new Date(b.DT)) .reduce((acc, val) => ({ ...acc, [val.group]: (acc[val.group] || []).concat(val) }), {}); // traverse sortedAscendingAndGrouped groups // and number the .name1 values, using // Array.forEach for (let entry in sortedAscendingAndGrouped) { sortedAscendingAndGrouped[entry] .forEach( (row, i) => row.name1 = (i+1) + "-" + row.name1); } console.log(sortedAscendingAndGrouped); function getArr() { return [{ id: 1, name: 'fryh', name1: 'ksdtfuy', group: '1208', DT: '2019-10-02T19:20' }, { id: 2, name: 'fthy', name1: 'xdhftdx', group: '1209', DT: '2019-10-02T09:27' }, { id: 3, name: 'qjkgjwer', name1: 'dhfdt', group: '1208', DT: '2019-10-02T09:24' }, { id: 4, name: 'jlkke', name1: 'atth', group: '1208', DT: '2019-10-02T19:20' }, { id: 5, name: 'gfhjm', name1: 'dryt', group: '1208', DT: '2019-10-02T09:28' }, { id: 6, name: 'iuit', name1: 'xx', group: '1209', DT: '2019-10-02T09:24' }, ]; }
 .as-console-wrapper { top: 0; max-height: 100% !important; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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