0

I have an array like below

[
{"st":"10:30,22:30","day":1},
{"st":"9:30,20:30","day":3},
{"st":"9:30,20:30","day":4},
{"st":"11:0,23:30","day":7},
{"st":"11:0,23:30","day":6},
{"st":"8:0,23:0","day":2},
{"st":"8:0,23:0","day":5}
]

I want to convert it like below

[
 {"st":"10:30,22:30","day":[1]},
 {"st":"9:30,20:30","day":[3,4]},
 {"st":"11:0,23:30","day":[7,6]},
 {"st":"8:0,23:0","day":[2,5]}
]

Based on the duplicate "st" values i want to merge those corresponding days. i was tried so many methods but i was unable to do. can anybody please help me out. Thanks in advance.

1
  • Can you give some examples of what you've tried? Commented Jul 5, 2016 at 7:42

3 Answers 3

2

One approach is the following:

var yourArray = [
{"st":"10:30,22:30","day":1},
{"st":"9:30,20:30","day":3},
{"st":"9:30,20:30","day":4},
{"st":"11:0,23:30","day":7},
{"st":"11:0,23:30","day":6},
{"st":"8:0,23:0","day":2},
{"st":"8:0,23:0","day":5}
];

var newArray =
Array.from(new Set(yourArray.map(elem => elem.st))) // get unique st's
    .map(elem => ({
        st: elem,
        day: yourArray.filter(el => el.st === elem).map(el => el.day) // get array of days
}));

console.log(newArray);

Note that this uses ES6 features and you need to use a transpiler (e.g. Babel) to use it in non-ES6 compliant browsers.

Resources

Sign up to request clarification or add additional context in comments.

Comments

1

This basic code might could help.

angular.module('MyApp', [])
  .controller('TestCtrl', function($scope) {
    $scope.originalArray = [{
      "st": "10:30,22:30",
      "day": 1
    }, {
      "st": "9:30,20:30",
      "day": 3
    }, {
      "st": "9:30,20:30",
      "day": 4
    }, {
      "st": "11:0,23:30",
      "day": 7
    }, {
      "st": "11:0,23:30",
      "day": 6
    }, {
      "st": "8:0,23:0",
      "day": 2
    }, {
      "st": "8:0,23:0",
      "day": 5
    }];
    $scope.result = [];

    (function() {
      angular.forEach($scope.originalArray, function(value) {
        var locateIndex = _.findIndex($scope.result, {
          'st': value.st
        });
        if (locateIndex === -1) {
          var currentDayValue = value.day;
          value.day = [currentDayValue];
          $scope.result.push(value);
          return;
        }

        $scope.result[locateIndex].day.push(value.day);
      });
    }).call()
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyApp" ng-controller="TestCtrl">
  original array
  <div>{{ originalArray }}</div>
  <br/>result
  <div>{{ result }}</div>
</div>

PS: I use lodash as JS utility library. (findIndex helper function)

1 Comment

Perfectly working for my scenario. Thank you so much
0

Create temp array for check duplicate st value by indexOf.Check st value exist in temp ,if not change your day value to array type .If exist push day to previous index and after deleted by splice

var object = [
{"st":"10:30,22:30","day":1},
{"st":"9:30,20:30","day":3},
{"st":"9:30,20:30","day":4},
{"st":"11:0,23:30","day":7},
{"st":"11:0,23:30","day":6},
{"st":"8:0,23:0","day":2},
{"st":"8:0,23:0","day":5}
];
var temp = [];
for(var i = object.length-1;i > 0;i--){
     if(temp.indexOf(object[i].st) != -1){
          var exist = temp.indexOf(object[i].st);
          object[exist].day.push(object[i].day);
          object.splice(i,1);
     }
     else{
         temp[i] = object[i].st;
         var d = object[i].day;
         object[i].day = [];
         object[i].day.push(d);
     }
}
console.log(object);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.