0

I want to loop through a javascript object passed in like this:

{familyid:434832,groupid:5332,programtypecode:'daycare'}

But I don't know the parameter names. There could be any number of parameters with various different names. I want to get the names of the parameters passed in, and of course also their values. How do I get that?

Vincent
  • 1,741
  • 23
  • 35

2 Answers2

1

Use Object.keys()

const o = {
  familyid: 434832,
  groupid: 5332,
  programtypecode: 'daycare'
}

keys = Object.keys(o)

// Do something with your keys, like
for (key of keys) {
 console.log(`${key} => ${o[key]}`)
}
msanford
  • 11,803
  • 11
  • 66
  • 93
1

If you want the result in form of array of array of arrays then use Object.entries

let obj = {familyid:434832,groupid:5332,programtypecode:'daycare'}
console.log(Object.entries(obj))

If you want to directly loop then use for..in

let obj = {familyid:434832,groupid:5332,programtypecode:'daycare'}

for(let k in obj){
  console.log(`${k}:${obj[k]}`)
}
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
  • This is a superior answer, but I'll leave mine anyway. I always forget about the entries tuple-ifier... – msanford Jul 05 '19 at 17:13