-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"
  }
]
2

3 Answers 3

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 });
   }
}
Sign up to request clarification or add additional context in comments.

3 Comments

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) codepen.io/jvmatl/pen/oyJpEg
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 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.
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);

Comments

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);

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.