2

I've researched for about a day and haven't had any luck. I'm trying to access the object properties to simply add it to a typed class in JavaScript. How can i accomplish this? This is the object im receiving:

enter image description here

And this my code resulting in the following error:

Object.keys(selected).forEach(function (key) {
        console.log(selected[key]);
    });

Error:

ERROR TypeError: Cannot convert undefined or null to object

Any help is most appreciated as I've come to here as a last resort.

selected looks like this: enter image description here

Object.keys(selected)... the selected in json string:

{
 "selected": [
    {
      "Id": 4,
      "Type": 0,
      "Image": "",
      "Name": "TestName",
      "Roles": "TestRole",
      "Facility": "TestFacil"
    }
  ]
}
7
  • 2
    It doesn't look like selected.selected exists. Try selected[0] and see if it works. You might also have to change the log line to ouput selected[0][key]. Commented Apr 27, 2018 at 19:30
  • Just tried, same result @CoreyOgburn Commented Apr 27, 2018 at 19:31
  • Maybe I'm not recreating your situation properly then. Here's a jsfiddle of my attempt to do so: jsfiddle.net/rct8y82L Commented Apr 27, 2018 at 19:33
  • 2
    please stringify the object and post it here instead of showing us pictures Commented Apr 27, 2018 at 19:35
  • updated question with 'selected' stringified. @Kristianmitk Commented Apr 27, 2018 at 19:41

2 Answers 2

3

The variable selected is an array, you can try this:

selected.forEach(
  item=>
    Object.keys(item).forEach(function (key) {
      console.log(item[key]);
    })
)

It could also be:

selected.selected.forEach(

In your question it's not clear what the name is of the value you are logging the json string of.

Sign up to request clarification or add additional context in comments.

1 Comment

This did it. It was selected.selected.forEach that worked for me. Thank you sir!
1

let data = {
 "selected": [
    {
      "Id": 4,
      "Type": 0,
      "Image": "",
      "Name": "TestName",
      "Roles": "TestRole",
      "Facility": "TestFacil"
    }
  ]
}


let arr = [];
for (var key in data) {
  arr.push(data[key]);
}

console.log(arr)

or you can use that if you wanna convert the whole object to arrays of arrays

        let data = {
         "selected": [
            {
              "Id": 4,
              "Type": 0,
              "Image": "",
              "Name": "TestName",
              "Roles": "TestRole",
              "Facility": "TestFacil"
            }
          ]
        }


    var arr = [];
    var i =0;
    for( var key in data){
      var innerArr = [key];
      arr.push(innerArr)
      for(var values in data[key]){
       for(var keys in data[key][values]){
         var innerArr3 = [keys];
         innerArr.push(innerArr3)
         innerArr3.push(data[key][values][keys])
        }
      }
    }
     console.log(arr)

2 Comments

This gives me the following error.. TypeError: selected.forEach is not a function
@tshoemake, i have added 2 options ,so to make the outer object as an array or to dig in the whole objects to arrays

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.