Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding object properties into a JSON object

I have a JSON object in this format.

[
    {
        "name": "schoolname",
        "line id": "0",
        "time": "4-5",
        "minage": "15",
        "maxage": "35"
    },
    {
        "name": "studentname1",
        "line id": "1",
        "class": "A"
    },
    {
        "name": "studentname2",
        "line id": "2",
        "class": "B"
    }
]

What I want to do

From a set of specified headers, get it from the "line id" : "0" and set it to the other items.

For Example: headers = ["time", "minage", "maxage"]

I get these from the "line id" : "0" and give it to others like this.

[
    {
        "name": "schoolname",
        "line id": "0",
        "time": "4-5",
        "minage": "15",
        "maxage": "35"
    },
    {
        "name": "studentname1",
        "line id": "1",
        "class": "A",
        "time": "4-5",
        "minage": "15",
        "maxage": "35"
    },
    {
        "name": "studentname2",
        "line id": "2",
        "class": "B",
        "time": "4-5",
        "minage": "15",
        "maxage": "35"
    }
]

and then remove the element with "line id" : "0", like this:

[
    {
        "name": "studentname1",
        "line id": "1",
        "class": "A",
        "time": "4-5",
        "minage": "15",
        "maxage": "35"
    },
    {
        "name": "studentname2",
        "line id": "2",
        "class": "B",
        "time": "4-5",
        "minage": "15",
        "maxage": "35"
    }
]

It is said that the 1st element would be a "line id" : "0".

What I've tried:

var headers = ["time", "minage", "maxage"]
var data = 
    [
        {
            "name": "schoolname",
            "line id": "0",
            "time": "4-5",
            "minage": "15",
            "maxage": "35"
        },
        {
            "name": "studentname1",
            "line id": "1",
            "class": "A"
        },
        {
            "name": "studentname2",
            "line id": "2",
            "class": "B"
        }
    ];

for(var i = 1; i < data.length; i++)//iterate over the data leaving the 1st line
{
    for(var j = 0; j < headers.length; j++)//add each header to the data lines
     {
        data[i][headers[j]] = data[0][headers[j]];
     }
}
data.splice(0,1);

Everything works fine and as intended. Is there a way to reduce the time-complexity of this and make it more efficient.

This now has a time complexity of O(n * m).

Is there a way around of adding these few objects to all the elements? As the key value pairs to be added for all the entries remain the same.

like image 795
Uma Kanth Avatar asked Jul 04 '15 06:07

Uma Kanth


1 Answers

You can use Object.defineProperties like

var arr = [{
    "name": "schoolname",
    "line id": "0",
    "time": "4-5",
    "minage": "15",
    "maxage": "35"
  }, {
    "name": "studentname1",
    "line id": "1",
    "class": "A"
  }, {
    "name": "studentname2",
    "line id": "2",
    "class": "B"
  }],
  headers = ["time", "minage", "maxage"];


function addHeaders(arr, headers) {
  var header = arr.splice(0, 1)[0],
    propObj = headers.reduce(function(acc, el) {
      acc[el] = {
        value: header[el],
        writable: true,
        enumerable: true
      };
      return acc;
    }, {});

  for (var i = 0, len = arr.length; i < len; i++) {
    Object.defineProperties(arr[i], propObj);
  }
  return arr;
}

document.getElementById('r').innerHTML = 'initial: ' + JSON.stringify(arr,null,2) + '<br/>';
document.getElementById('r').innerHTML += 'result: ' + JSON.stringify(addHeaders(arr, headers),null,2);
<pre id="r"></pre>
like image 150
Grundy Avatar answered Sep 28 '22 10:09

Grundy