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.

5
  • what happens in the edge case where arr.length % 3 !== 0 Commented Mar 31, 2017 at 19:14
  • 1
    phase, phase, phase ... same keys? Commented Mar 31, 2017 at 19:14
  • 5
    you can not use same key for many object properties, so this will not work. Commented Mar 31, 2017 at 19:15
  • you need to make pk the unique Commented Mar 31, 2017 at 19:16
  • 1
    For what it's worth, you aren't showing us any JSON. JSON is a text format which requires the keys to be surrounded in double quotes. What you're showing us an array of objects. Commented Mar 31, 2017 at 19:20

2 Answers 2

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>

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

Comments

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);

1 Comment

Would you be so kind as to explain what your code is doing and how it solves the problem?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.