1

We need to transfer the data from the javascript object to the left into an array. Initialize a new array called userDataArray and add all of the data from the object into the array by calling it from the object using the number keys that have been added to the object.

When you are done, you should have the same data from the object that has already been written, but in an array, without having to retype each of the variables separately.

var userData = {
  1: true,
  2: true,
  3: "00QRA10",
  4: "slimer42",
  5: "FFASN9111871-USN16"
};

var userDataArray = [0,1,2,3,4];
6
  • 5
    it looks like homework, what have you tried? Commented Oct 26, 2017 at 16:22
  • It is a homework challenge. The second var is my answer. var userDataArray = [0,1,2,3,4]; Commented Oct 26, 2017 at 16:23
  • I don't understand "from the javascript object to the left into an array - What exactly is to transfer to the left? And did you read the documentation about the methods of object? Commented Oct 26, 2017 at 16:35
  • I'm voting to close this question as off-topic because it is clearly a homework. Commented Oct 26, 2017 at 17:06
  • Sorry, didn't even think about that wording. To the left is how it is displayed on the assignment. The object on the left is var userData = { 1: true, 2: true, 3: "00QRA10", 4: "slimer42", 5: "FFASN9111871-USN16" }; Commented Oct 26, 2017 at 17:07

3 Answers 3

1

You can use new function like Object.values to get values from object, and Object.keys to get keys from the object.

var userData = {
  1: true,
  2: true,
  3: "00QRA10",
  4: "slimer42",
  5: "FFASN9111871-USN16"
};

var userDataArray = Object.values(userData)
Sign up to request clarification or add additional context in comments.

Comments

0

Not sure if this is what you want but...

var userData = {
  1: true,
  2: true,
  3: "00QRA10",
  4: "slimer42",
  5: "FFASN9111871-USN16"
};

var outputArray = [];
for (var i = 0; i < Object.keys(userData).length; i++) {
  outputArray.push(userData[Object.keys(userData)[i]]);
}
console.log(outputArray);

taking it apart: Object.keys(userData) is the list (array) of key names. In this case it is 1, 2, 3, 4, and 5. outputArray.push() adds an element to the array. Object.keys(userData)[i] is the name of the object element that we're on, and userData[Object.keys(userData)[i]] is just the element that we're on. So every time we go through the for loop, we add another element to the array.

Your example uses 1, 2, 3, 4, and 5 for their element names though, so something like this might work better:

var userData = {
  1: true,
  2: true,
  3: "00QRA10",
  4: "slimer42",
  5: "FFASN9111871-USN16"
};

var outputArray = [];
for (var i = 0; i < Object.keys(userData).length; i++) {
  outputArray.push(userData[i+1]);
}
console.log(outputArray);

In this example, instead of taking the element name out of the key array we assume that it is a number and that the numbers are in a sequence that starts with 1.

The reason why your solution didn't work

is because you didn't call the numbers from the object. You would've had to use userData.1, userData.2, userData.3, etc.

Comments

0

You can loop through an object and use the keys to get the values.

var userData = {
  1: true,
  2: true,
  3: "00QRA10",
  4: "slimer42",
  5: "FFASN9111871-USN16"
};

var userDataArray = [];

for (var key in userData) {
  userDataArray.push(userData[key])
}

console.log(userDataArray)

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.