1

I have the following object of arrays: hoW to loop through it and get the values of test and body individually? in javascript? used 2 for loops but nothing consoles out?

myobj: {
  foo: [
    {
      test: "jhaskd",
      body: "haskjdh",
    }
  ],
  bar: [
    {
      test: "jhaskd",
      body: "haskjdh",
    }
  ],
}
P.S.
  • 15,970
  • 14
  • 62
  • 86
monkeyjs
  • 604
  • 2
  • 7
  • 29

3 Answers3

1

Like This?

let myobj =  {
  foo: [
    {
      test: "jhaskd",
      body: "haskjdh",
    }
  ],
  bar: [
    {
      test: "jhaskd",
      body: "haskjdh",
    }
  ],
}

let level_1_keys = Object.keys(myobj)
console.log(level_1_keys) //['foo', 'bar']
let tests = level_1_keys.map(function(l_1_key){
    return myobj[l_1_key][0]["test"]
})
let bodies = level_1_keys.map(function(l_1_key){
    return myobj[l_1_key][0]["body"]
})
console.log(tests) //['jhaskd','jhaskd']
console.log(bodies) //['haskjdh', 'haskjdh']
WouldBeNerd
  • 629
  • 11
  • 29
0

Here is an example:

var myobj = {
  foo: [
    {
      test: "test 1",
      body: "body 1",
    }
  ],
  bar: [
    {
      test: "test 2",
      body: "body 2",
    }
  ],
}

var tests = [];
var bodies = [];

for(key in myobj) {
  if(myobj.hasOwnProperty(key)) {
    tests.push(myobj[key][0].test)
    bodies.push(myobj[key][0].body)
  }
}

console.log(tests);
console.log(bodies);

UPDATE

"Instead of tests[] and bodies[] - i need foo[] , bar[]"

var myobj = {
  foo: [
    {
      test: "test 1",
      body: "body 1",
    }
  ],
  bar: [
    {
      test: "test 2",
      body: "body 2",
    }
  ],
}

for(key in myobj) {
  if(myobj.hasOwnProperty(key)) {
    window[key] = []
    window[key].push(myobj[key][0].test)
    window[key].push(myobj[key][0].body)
  }
}

console.log(foo);
console.log(bar);

If anything isn't clear - feel free to ask.

P.S.
  • 15,970
  • 14
  • 62
  • 86
  • Please wrap `myobj[key]` with `if(myobj.hasOwnProperty(key))` to prevent accidently pick up metadata. – cyr_x Sep 29 '17 at 00:25
  • @cyrix, oh, yes, it's a good practice, thank you! – P.S. Sep 29 '17 at 00:25
  • @CommercialSuicide I need foo and bar arrays to be separated out, not based on the values - so instead of tests[] and bodies[] - i need foo[] , bar[] – monkeyjs Sep 29 '17 at 00:32
  • @monkeyjs, is it OK now? – P.S. Sep 29 '17 at 00:41
  • @CommercialSuicide how can u access console.log(foo);? and what is window[key] – monkeyjs Sep 29 '17 at 03:31
  • @monkeyjs, we can store global variables to `window` object like properties to the regular object. `window[key]` will create a global variable in `window` object and the name of the variable will be the current key of `myobj` at current iteration. So variables were created like this: `window[foo]` on first iteration and `window[bar]` on second iteration. And to get access to variables created through `window` object, we can just turn to them like to regular global variables, so `console.log(foo);` and `console.log(bar);` will work. – P.S. Sep 29 '17 at 03:44
  • does not work for me @CommercialSuicide – monkeyjs Sep 29 '17 at 05:17
  • @monkeyjs, what exactly doesn't work? Looks like works OK in the snippet – P.S. Sep 29 '17 at 05:20
  • snippet works, but in reactjs code i am getting ReferenceError: key is not defined @CommercialSuicide – monkeyjs Sep 29 '17 at 05:37
  • @CommercialSuicide may be it is becos of window - can we do this without window object? – monkeyjs Sep 29 '17 at 05:38
  • @monkeyjs, can't really say, I don't know another way without `window`. If you're using any frameworks, there should be a way to use `window` object, but how to do that is depends in your framework. – P.S. Sep 29 '17 at 05:44
  • @monkeyjs, or there should be an alternative way, but as I said, it depends on your framework – P.S. Sep 29 '17 at 05:50
0

There are many ways to do this, but if you don't actually care about the keys of the parent object, this should do it:

for (let list of Object.values(myobj)) {
  for (let obj of list) {
    console.log(obj.test)
    console.log(obj.body)
  }
}

This assumes that every element in the parent object is an array, but if that is not the case you can simply add an if-clause. See https://repl.it/LoL1

Sven
  • 5,155
  • 29
  • 53