2

hi all i am using javascript i am trying to push object values into one array here i attached my code

var data = [{
            BuildingID: "56", FloorId: "65", volume: [{

                one: 12,
                two: 15,
                three: 12
            }]
        }]

var MainInfo=[];
            for (i = 0; i < data.length; i++) {

                var obj = {}
                obj.BuildingID = data[i].BuildingID;
                obj.FloorId = data[i].FloorId;
                obj.volume = data[i].volume[0];
                MainInfo.push(obj);
  
            }

it's working but not fully applicable here i mentioned the excepted output what should i need

Current output

enter image description here

Expected output

enter image description here

Note:in data volume is dynamic help how to do this

2
  • 2
    (It is so nice to finally meet JavaScript in person) :-) Commented Jun 6, 2017 at 9:01
  • i have updated my question:) @ trincot Commented Jun 6, 2017 at 9:05

8 Answers 8

3

This should work for you

var data = [
	{
		BuildingID : "56",
		FloorId : "65",
		volume : [
			{
				one : 12,
				two : 15,
				three : 12
			}
		]
	}
];
var MainInfo = [];
for (i = 0; i < data.length; i++) {
	var obj = {};
	obj.BuildingID = data[i].BuildingID;
	obj.FloorId = data[i].FloorId;
	Object.getOwnPropertyNames(data[i].volume[0]).forEach(function (key) {
			obj[key] = data[i].volume[0][key];
	});
	MainInfo.push(obj);
}
console.log(MainInfo);

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

Comments

2

loop volume and get keys

var data = [{
            BuildingID: "56", FloorId: "65", volume: [{

                one: 12,
                two: 15,
                three: 12
            }]
        }];

var MainInfo=[];
  for (var i = 0; i < data.length; i++) {

      var obj = {};
      obj.BuildingID = data[i].BuildingID;
      obj.FloorId = data[i].FloorId;
     
      for(var j in data[i].volume[0]){
     
        obj[j]= data[i].volume[0][j];
      }
      MainInfo.push(obj);

  }
            
            console.log(MainInfo);

1 Comment

It is usually a good idea to skip potential inherited properties with a hasOwnProperty check or an equivalent.
2

There are a lot of correct answers already present however you can use ES6 Arrow function to achieve this with minimum syntax.

var MainInfo = data.map( x=>{ return {
        "BuildingID" : x.BuildingID,
        "FloorId" : x.FloorId,
        "one" : x.volume[0]["one"],
        "two" : x.volume[0]["two"],
        "three" : x.volume[0]["three"],
    }})

More about arrow functions here

Comments

1

If you want this expected output just replace the 2nd part by

var MainInfo=[];
        for (i = 0; i < data.length; i++) {

            var obj = {}
            obj.BuildingID = data[i].BuildingID;
            obj.FloorId = data[i].FloorId;
            obj.one = data[i].volume[0].one;
            obj.three = data[i].volume[0].three;
            obj.two = data[i].volume[0].two;

            MainInfo.push(obj);

        }

1 Comment

Note:in data volume is dynamic help how to do this
1

Try with Object.key() and Object.values() function .And Array#forEach used for iterate the array .Don't hardcore with volume[0] .batter iterate with forEach

var data = [{ BuildingID: "56", FloorId: "65", volume: [{ one: 12, two: 15,   three: 12}] }]

var MainInfo=[];
            for (i = 0; i < data.length; i++) {
                var obj = {}
                obj.BuildingID = data[i].BuildingID;
                obj.FloorId = data[i].FloorId;
                data[i].volume.forEach(function(a,c){
                Object.keys(data[i].volume[c]).forEach(function(a,b){
                  obj[a] = Object.values(data[i].volume[c])[b]
                })
                })
                MainInfo.push(obj);
            }
            console.log(MainInfo)

4 Comments

Not quite sure what the point of using Object.values could be, when you can directly access the original object, especially given that Object.values support is very limited.
@jcaron For op requirement Object.values is enough .Object.value array is same length of the object.key array that why i was used .and have same position of the value with respected key array
Using Object.values overcomplicates things for no reason. You write Object.values(data[i].volume[c])[b] when you could write data[i].volume[c][a]. Also, there are lots of browsers not supporting it.
Also you're re-using the same variable (a) in two nested loops, which is prone to confusion. And your indentation is incorrect.
0

You can use a function like this one:

function copyProperties(sourceObject, destinationObject)
{
    var k;
    for (k in sourceObject)
    {
       if (sourceObject.hasOwnProperty(k))
         destinationObject[k] = sourceObject[k];
    }
}

Note that you haven't quite described what should happen if there's more than one object in the volume array.

Comments

0

You could use Object.assign for assigning all properties to another object.

var data = [{ BuildingID: "56", FloorId: "65", volume: [{ one: 12, two: 15, three: 12 }] }],
    mainInfo = [],
    i, obj;

for (i = 0; i < data.length; i++) {
  obj = {}
  obj.BuildingID = data[i].BuildingID;
  obj.FloorId = data[i].FloorId;
  mainInfo.push(Object.assign(obj, data[i].volume[0]));
}

console.log(mainInfo);

2 Comments

Note that Object.assign is not supported by IE or the majority of Android browsers, you'll need a polyfill for those.
@jcaron, right, but the new standard is ES6 with Object.assign. it is still possible to iterate wanted properties and assign by hand, but to know, theres a new method to directly assign a whole bunch of properties, it is an advantage.
0

If you are using jQuery, you can use $.extend(obj, volume[0])

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.