1

I am trying to copy an object userinfo into an array. I keep getting undefined when I try to console log the new array. Any help will be appreciated!

JS:

var JSONstring = [{
  "name": "test",
  "properties": {
    "age": "19r",
    "userinfo": {
      "city": "Dallas",
      "state": "Texas"
    }
  }
}];

$(document).ready(function() {
  var userinfo = {};
  for (var i = 0; i < JSONstring.length; i++) {
    var user = JSONstring[i].properties.userinfo;
    if (user === undefined) {
      continue;
    } else if (userinfo[user] === undefined) {
      userinfo[user] = [];
      userinfo[user].push(i);
    } else {
      userinfo[user].push(i);
    }
  }
  console.log(userinfo[0]);
});

JSFiddle Demo

7
  • userinfo[user] doesn't make a whole lot of sense, user is either undefined or an object. object keys are always strings. Commented Apr 21, 2017 at 21:09
  • So there are a few problems with object types in your example. Your JSONstring var is an object, it does not have a .length member. Your userinfo "array" is also an object. Commented Apr 21, 2017 at 21:11
  • Please not that the variable name JSONstring is extremely misleading since the value of the variable is an array, not a string (containing JSON). Calling JavaScript objects "JSON" is a (unfortunately) common mistake. Commented Apr 21, 2017 at 21:11
  • @FelixKling ok will keep that in mind Commented Apr 21, 2017 at 21:12
  • @Ken: NVM then :) Commented Apr 21, 2017 at 21:13

1 Answer 1

2

Actually you have several problems in your current code:

  • Writing userinfo[user] is incorrect, if you want to access userproperty of userinfoobject you need to give it a string like userinfo["user"] or just directly userinfo.user.
  • In the end you are trying to log userinfo[0] which is undefined because userinfo is an object and not an array you need to change it to : console.log(userinfo.user[0]).
  • If you want to store the user object in the array you should replace push(i) with push(user).

Demo:

I tried to refactor your code so it makes sense, here's what you need:

var JSONstring = [{
  "name": "test",
  "properties": {
    "age": "19r",
    "userinfo": {
      "city": "Dallas",
      "state": "Texas"
    }
  }
}];

$(document).ready(function() {
  var userinfo = {};
  for (var i = 0; i < JSONstring.length; i++) {
    var user = JSONstring[i].properties.userinfo;
    if (!user) {
      continue;
    } else if (!userinfo.user) {
      userinfo.user = [];
      userinfo.user.push(user);
    } else {
      userinfo.user.push(user);
    }
  }
  console.log(userinfo.user[0]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Note:

The condition in the if block if (userinfo[user] === undefined) can simply be written if(!userinfo.user)

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

2 Comments

Why is the console.log coming as 0 now?
Because it takes i in the loop, we should push(user), I edited my answer.

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.