user2128
user2128

Reputation: 640

Iterate over an array and store values as key and value of an object using angular js

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.

My array looks like: enter image description here

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

Answers (1)

Pop-A-Stash
Pop-A-Stash

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

Related Questions