0

I'm having an array of object,in which I'm storing the billkey and billvalue as attributes. I want billkey to be the key and billvalue to be the value of that particular key.

var log=[
{
  billkey:"Name",
  billvalue:"ABC"
},
{ 
  billkey:"Department",
  billvalue:"Computer"
}
{
  billkey:"Name",
  billvalue:"XYZ"
},
{ 
  billkey:"Department",
  billvalue:"Electrical"
}];

And I want to convert it into this format:

var log=[
    {
      Name:"ABC",
      Department:"Computer"
    },
    { 
      Name:"XYZ",
      Department:"Electrical"
    }];
5
  • Write custom function for this Commented Jan 11, 2017 at 5:05
  • 1
    What is the logic which says Department for ABC is Computer? Commented Jan 11, 2017 at 5:06
  • what language do you use. i can support you with Pyhon Commented Jan 11, 2017 at 5:07
  • 1
    @DinuDuke: Looks at the tags : JavaScript Commented Jan 11, 2017 at 5:08
  • i can suggest you to go with replacing the key and value using Regx look this link this might help you...stackoverflow.com/questions/1162529/javascript-replace-regex Commented Jan 11, 2017 at 5:23

3 Answers 3

0

How about this simple solution. Hope it helps!

var log=[
{
  billkey:"Name",
  billvalue:"ABC"
},
{ 
  billkey:"Department",
  billvalue:"Computer"
},
{
  billkey:"Name",
  billvalue:"XYZ"
},
{ 
  billkey:"Department",
  billvalue:"Electrical"
}];

var arr = [];
var finalObj = [];

for(var i in log){
	var someObject = log[i];
	for(var j in someObject){
	  arr.push(someObject[j]);
	}
}

for(var k = 0; k < arr.length; k+=4){
      finalObj.push({
      Name: arr[k+1],
      Department: arr[k+3]
    });
}

console.log(finalObj);

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

Comments

0

create the result using forloop

 // store the values
  var logs=[];
  var log=[
{
  billkey:"Name",
  billvalue:"ABC"
},
{ 
  billkey:"Department",
  billvalue:"Computer"
},
{
  billkey:"Name",
  billvalue:"XYZ"
},
{ 
  billkey:"Department",
  billvalue:"Electrical"
},
];

loop the first array

for (i = 0; i < log.length; i++) {
    // create empty variable for storing the values
    var index = new Array();
    // insert the first index value to key
    index[log[i].billkey] = log[i].billvalue
    // insert the second index value to key 
    index[log[i+1].billkey] = log[i+1].billvalue
    // insert the result in to new array
    logs.push(index);
    // increment the i with 1 
    i=i+1;
}
console.log(logs);

1 Comment

for (i = 0; i < log.length; i+=2) would be much better.... that you can avoid i=i+1; ( which is equivalent to i++;)
0

You could use Array#reduce and use the remainder operator as witch for using either the last object or create a new one.

var log = [{ billkey: "Name", billvalue: "ABC" }, { billkey: "Department",  billvalue: "Computer" }, { billkey: "Name", billvalue: "XYZ" }, { billkey: "Department", billvalue: "Electrical" }],
    result = log.reduce(function (r, a, i) {
        var o = {};
        if (i % 2) {
            r[r.length - 1][a.billkey] = a.billvalue;
        } else {
            o[a.billkey] = a.billvalue;
            r.push(o);
        };
        return r;        
    }, []);

console.log(result);

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.