-2

I have nested objects within an object as such:

var parentObj = { 
   key1: {
     a: true,
     b: 2
   },
   key2: {
     a: false,
     b: 2
   },
   key3: {
     a: true,
     b: 2
   }
}

I'm looking to create an array of objects from key values, if one of the values in the nested objects is true, and that also includes the keys as a [key,value] pair as such:

parentobj = [
  {
    a: true,
    b: 2,
    c: "key1" 
  },
  {
    a: true,
    b: 2,
    c: "key3"
  }
]
JVMATL
  • 2,064
  • 15
  • 25
  • 1
    Possible duplicate of [Convert object's properties and values to array of key value pairs](https://stackoverflow.com/questions/14615669/convert-objects-properties-and-values-to-array-of-key-value-pairs) – vahdet Jun 29 '18 at 14:06
  • Welcome to StackOverflow. Take the [tour](https://stackoverflow.com/tour) and have a look into [how to ask good questions](https://stackoverflow.com/help/how-to-ask) to maximize your chances of getting an answer. In this case, I wonder what you have tried already? Did you make an attempt to solve this problem yourself? If so, where are you stuck? – WhoIsJack Jun 29 '18 at 14:17

3 Answers3

1

Just use a for...in loop, like so:

var myArray = [];
for(var key in parentObj){
   var childObj = parentObj[key];
   if(childObj.a) {
      myArray.push({a: childObj.a, b: childObj.b, c: key });
   }
}
Byron Jones
  • 702
  • 5
  • 11
  • Not sure why this answer got a downvote? Seems to do what the author asked, (once you fix the typos in the original poster's JSON) https://codepen.io/jvmatl/pen/oyJpEg/ – JVMATL Jun 29 '18 at 14:31
  • if its not typo your ans is {a: true, b: 2, c: "key3"} so then we have to make a array for that ! – Onk_r Jun 29 '18 at 14:52
  • @Onk_r the OP's question states that the requirement is that the resulting objects "also includes the **keys**. OP's omitted the quotes around the key strings in the sample expected output, but as demonstrated in your comment, it's the right answer. – Byron Jones Jun 29 '18 at 15:20
0

First thing to mention here is

Associative arrays do not exist in Javascript

you can see here

In your example "key1" is duplicated so if you want to store like then then you have to use Array for that like if one of the values in the nested objects is true

var parentObj = {
   key1: [
       {
         a: true,
         b: 2
       },
       {
         a: false,
         b: 2
       }
   ],
   key3: [
       {
         a: true,
         b: 2
       }
   ]
};

In that case what you want is done like this !

var parentObj = {
   key1: [
       {
         a: true,
         b: 2
       },
       {
         a: false,
         b: 2
       }
   ],
   key3: [
       {
         a: true,
         b: 2
       }
   ]
};
var myArray = [];
for(var key in parentObj){
   var childObj = parentObj[key];
   var res = childObj.filter(function(element){
       return  element.a == true;
   });
   for(ele in res){
       res[ele].c = key;
   }
   if(res.length > 0){
       // if you want any of element having true property
       myArray.push(res[0]);
   }
}
console.log(myArray);
Onk_r
  • 836
  • 4
  • 21
0

A simple one-line solution: extract keys from the object and iterate over them while creating respective objects and filter out the falsy ones:

var parentObj = { 
   key1: {
     a: true,
     b: 2
   },
   key2: {
     a: false,
     b: 2
   },
   key3: {
     a: true,
     b: 2
   }
};

var result = Object.keys(parentObj).map(k => (Object.assign({}, parentObj[k], {c: k}))).filter(({a}) => a);

console.log(result);
Leonid Pyrlia
  • 1,594
  • 2
  • 11
  • 14