0

I have an existing array that I would like to reformat in JS. Here is the existing array where each item is an object:

[
 {
  end_time:"7.14",
  pk:45065,
  start_time:"4.51",
  text:"Lorem Ipsum"
 },
 {
  end_time:"9.00",
  pk:45066,
  start_time:"7.14",
  text:"Lorem Ipsum Something"
 },
 {
  end_time:"13.09",
  pk:450667 ,     
  start_time:"9.00",
  text:"Lorem Ipsum Something"
 }, 
 {
  end_time:"17.01",
  pk:45068,
  start_time:"13.09",
  text:"Lorem Ipsum"
 },
 {
  end_time:"25.10",
  pk:45069,
  start_time:"17.01",
  text:"Lorem Ipsum Something"
 },
 {
  end_time:"28.06",
  pk:450670 ,     
  start_time:"25.10",
  text:"Lorem Ipsum Something"
 },
]

I would like to create a new array of objects where for every three objects in old array becomes one object in the new array like so:

 [
  segment: {
    phrase: {
      end_time:"7.14",
      pk:45065,
      start_time:"4.51",
      text:"Lorem Ipsum"
    },
    phrase: {
      end_time:"9.00",
      pk:45066,
      start_time:"7.14",
      text:"Lorem Ipsum Something"
     },
     phrase: {
      end_time:"13.09",
      pk:450667 ,     
      start_time:"9.00",
      text:"Lorem Ipsum Something"
     }
  },
  segment {
    phrase: {
      end_time:"17.01",
      pk:45068,
      start_time:"13.09",
      text:"Lorem Ipsum"
    },
    phrase: {
      end_time:"25.10",
      pk:45069,
      start_time:"17.01",
      text:"Lorem Ipsum Something"
    },
    phrase: {
      end_time:"28.06",
      pk:450670 ,     
      start_time:"25.10",
      text:"Lorem Ipsum Something"
    },
  }
]

What I am struggling with most is how to pull out every three items and push to the new segment object inside a map or loop I guess. I am not sure the most efficient way to go about this. Any help is much appreciated.

PK_info
  • 87
  • 1
  • 7

2 Answers2

0

Reduce function drops every object from the json array inside a new object, where the key is phrase and its value is that specified object. Then, just assign the result to a newly created object as the value of the segment key.

var json = [{end_time:"7.14",pk:45065,start_time:"4.51",text:"Lorem Ipsum"},{end_time:"9.00",pk:45066,start_time:"7.14",text:"Lorem Ipsum Something"},{end_time:"13.09",pk:450667,start_time:"9.00",text:"Lorem Ipsum Something"},{end_time:"17.01",pk:45068,start_time:"13.09",text:"Lorem Ipsum"},{end_time:"25.10",pk:45069,start_time:"17.01",text:"Lorem Ipsum Something"},{end_time:"28.06",pk:450670,start_time:"25.10",text:"Lorem Ipsum Something"}], 
  res = json.reduce(function(s,a){
    obj = {};
    obj.phrase = a;
    s.push(obj);
    return s;
  }, []);
  
  var object = {};
  object.segment = res;
  var result = [object];
  
  console.log(result);
kind user
  • 40,029
  • 7
  • 67
  • 77
0
  1. Your wanted result should be object as arrays have no string keys.
  2. Objects should have unique keys, so the proposed solution appends number suffixes to 'segment' and 'phrase' keys.
  3. The code:

var arr = [
  {
    end_time:"7.14",
    pk:45065,
    start_time:"4.51",
    text:"Lorem Ipsum"
  },
  {
    end_time:"9.00",
    pk:45066,
    start_time:"7.14",
    text:"Lorem Ipsum Something"
  },
  {
    end_time:"13.09",
    pk:450667 ,     
    start_time:"9.00",
    text:"Lorem Ipsum Something"
  }, 
  {
    end_time:"17.01",
    pk:45068,
    start_time:"13.09",
    text:"Lorem Ipsum"
  },
  {
    end_time:"25.10",
    pk:45069,
    start_time:"17.01",
    text:"Lorem Ipsum Something"
  },
  {
    end_time:"28.06",
    pk:450670 ,     
    start_time:"25.10",
    text:"Lorem Ipsum Something"
  },
];
var obj = {};
arr.forEach(arrayTransformClosure(obj));
document.getElementById('output').innerHTML = JSON.stringify(obj, null, 2);
// Functions
function arrayTransformClosure(obj) {
  var phrase = 0;
  var segment = 1;
  return function (elem) {
    ++phrase;
    if (phrase == 1) {
      obj['segment_' + segment] = {
        ['phrase_' + phrase]: elem
      };
    } else if (phrase <= 3) {
      obj['segment_' + segment]['phrase_' + phrase] = elem;
    } else {
      phrase = 1;
      ++segment;
      obj['segment_' + segment] = {
        ['phrase_' + phrase]: elem
      };
    }
  };
}
<pre id="output"></pre>
a1111exe
  • 641
  • 4
  • 18