-1

One Object inside another Object and inside an Array.The data structure is

{
"_id" : "BXPqcoCYSXPQNkq9S",
"client_id" : "REzch3X67Efm2bCri",

"locations" : [ 
    "kochi", 
    "trivandrum"
],
"tags" : {
    "status" : [ 
                        "Active", 
                        "Paused"
    ],
   "category" : [ 
                        "Display", 
                        "Search"
    ]




 }
}

My Question is How to get Output is status and category ?

I am Alredy used to find active and Paused Method is

profile.map((data)=>(

  console.log(data.tags['status']) //Active 
                                  //Paused
  ))

How to get value without name Specifeid status ?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Aboobakkar P S
  • 796
  • 1
  • 8
  • 28

2 Answers2

2

You can use Object.keys()

The Object.keys() method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

Once you have the keys, you can iterate over them using for loop.

const obj = {"_id":"BXPqcoCYSXPQNkq9S","client_id":"REzch3X67Efm2bCri","locations":["kochi","trivandrum"],"tags":{"status":["Active","Paused"],"category":["Display","Search"]}}

var result = Object.keys(obj.tags)
console.log('Keys: ', result);

Object.keys(obj.tags).forEach(k => console.log('Values for key', k, ':', obj.tags[k]));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
0

you can loop on keys inside any object in javascript

Check the code below:

 profile.map((data)=>(
  var tags = data.tags;
  for(tag in tags)
  {
      if(tags.hasOwnProperty(tag))
      {
          console.log(tag);//tag
          console.log(tags[tag]);//inner values
      }
  }
));

Hint hasOwnProperty is used to make sure that the keys are actually owned by the object not by its prototype.

Amr Labib
  • 3,995
  • 3
  • 19
  • 31
  • If `profile` is an object, how are you using `map` on it? – Andy Nov 10 '17 at 13:45
  • 1
    ! how do you know profile is an object ? .. this is not mentioned in the question ... `data` is an object ... profile is a blackbox that is not related to the question – Amr Labib Nov 10 '17 at 13:48