Reputation: 640
I have an array of objects of length 535. I want to loop through that array and extract the values inside those objects and store it in a single object while also setting key and value of that single object.
Lets call this array: responseData and every object in the responseData looks like:
{
"Tag": "bgnt",
"Label": "BeginTime",
"Description": "The time the event began to occur, if the event source reports events indicating the beginning of length transactions.",
"Type": "3",
"Taxonomy": "false",
"Category": "Uncategorized",
"Tokenized": "false"
}
What I want to do is loop through all objects, and store all objects into a single object say mainObject. And also define key and values of mainObject as mainObject.key = responseData[i].tag; mainObject.value = responseData[i].Label;
How do I : 1. Create a mainObject? 2. Assign key and value of mainObject ?
Any kind of help is appreciated. Thanks!
Upvotes: 0
Views: 375
Reputation: 6652
You don't need angular for this. Use bracket notation to set the key using the item tag
var myObject = {};
myArray.forEach(function(item) {
myObject[item["Tag"]] = item["Label"];
});
console.log(myObject["bgnt"]); // "Begin Time"
Upvotes: 1