0

I have bookingData.roomList,

bookingData.roomList = [{id : <>, roomCategory : Single, number : 2}, {id:<>,roomCategory : Double, number : 1}]

And i want to make a new values like roomListData

roomListData = {<uuid> : {roomCateghory : Single, number : 2}, 
                <uuid1> : {roomCateghory : Double, number : 2} }

please some one help me to solve this

1
  • yeah i checked it's working fine. thanks for your support bro. thanks for everybody Commented May 4, 2017 at 12:13

4 Answers 4

3

I think This is what you need. To use a property value as property label you use []

var roomList = [{id: 4514,roomCategory: "Single", number: 2}, 
                {id: 4542,roomCategory: "Double", number: 1}];

var newJson = [];
roomList.forEach(function(item) {
  newJson.push({
  [item.id] :{
        "roomCategory": item.roomCategory,
        "number": item.number
        }
  });
});
console.log(newJson);
Sign up to request clarification or add additional context in comments.

1 Comment

How to iterate in the 'newJson' that has been created?
1

1. using JavaScript for...in loop :

var bookingData = {
	"roomList": [{
		id: 1,
		roomCategory: "Single",
		number: 2
	}, {
		id: 2,
		roomCategory: "Double",
		number: 1
	}]
};

var roomListData = {};

for(var i in bookingData.roomList) {
  roomListData[bookingData.roomList[i].id] = {
    "roomCategory" : bookingData.roomList[i].roomCategory,
    "number" : bookingData.roomList[i].number
  }
}

console.log(roomListData);

2. using Array.map() method with ES6 Arrow function :

var bookingData = {
	"roomList": [{
		id: 1,
		roomCategory: "Single",
		number: 2
	}, {
		id: 2,
		roomCategory: "Double",
		number: 1
	}]
};

var roomListData = {};

bookingData.roomList.map(item =>
  roomListData[item.id] = {
    "roomCategory" : item.roomCategory,
    "number" : item.number
  });

console.log(roomListData);

3. using ES6 Spread assignment :

var bookingData = {
	"roomList": [{
		id: 1,
		roomCategory: "Single",
		number: 2
	}, {
		id: 2,
		roomCategory: "Double",
		number: 1
	}]
};

function createObj(a,b) {
  var roomListData = {};
  roomListData[a.id] = a,delete a.id;
  roomListData[b.id] = b,delete b.id;
  return roomListData;
}

var res = createObj(...bookingData.roomList);
console.log(res);

Comments

1

`

var roomList = [{id : 1, roomCategory : "Single", number : 2}, {id:2,roomCategory : "Double", number : 1}];
var roomListData = {};
for(var i=0;i< roomList.length ;++i){
    var temp =  roomList[i];
    var id = temp.id;
    delete temp.id;
    roomListData[id]=temp;
}
console.log(roomListData);
`

Comments

0

I assuming by uuid1 you mean a new uuid, not 'uuid' with the number 1 appended.

bookingData.roomList = [{id : <>, roomCategory : Single, number : 2}, {id:<>,roomCategory : Double, number : 1}]

roomList = {};
bookingData.forEach(function(element, index, array) {
    var uuid = GenerateNewUUID();
    // or uuid = element.id;
    delete element.id;
    roomList[uuid] = element;
});

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.