0

Can you please let me know if it is possible to pass an array into a JavaScript object like this?

var content = {
                item1: "John",
                item2: "Doe",
                item3: { "item3_1", "item3_2", "item3_3"   }
              }

console.log(content);

Can you please let me know how I can access or update the items, or if not, can you please let me know how I can create a data format (Array of Array for example) to store these data?

2 Answers 2

2

The syntax for defining an array on a JavaScript object looks like this:

var content = {
  item1: "John",
  item2: "Doe",
  item3: ["item3_1", "item3_2", "item3_3"]
};

You're not "passing" the array into the object, you're simply defining one of the properties to be an array. Any nested data inside that would take the same form that an object or another array would:

var foo = {
  arr: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
};

The important thing to remember is that you use '{}' for object syntax, and '[]' for array syntax. You can also have objects inside that array:

var bar = {
  obj_arr: [
      {
        name: "Tim",
        age: 14,
      },
      {
        name: "Bob",
        age: 36
      },
      {
        name: "Sue",
        age: 73
      }
    ]
  };

To access one of the individual elements from the array property, use this syntax:

content.item3[0] for "item3_1"
content.item3[1] for "item3_2"

or, alternatively:

content["item3"][0] for "item3_1"
content["item3"][1] for "item3_2"

although this syntax is less common when the property name is known ahead of time. To loop through the items in that array, you can use a for loop:

for (var i = 0, l = content.item3.length; i < l; i += 1) {
    console.log(content.item3[i]);
}

This is how you would console.log the items out - if you wanted to do something else with them, you would need to substitute that action for the console.log call.

Here's an updated fiddle that does what I believe you're looking for: http://jsfiddle.net/qobo98yr/5/

This is the code for printing everything out in the content object to the console:

var outputString = "";
for (var prop in content) {
  outputString += (content[prop] + " ");
}
console.log(outputString);
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks kinakuta, just can you please let me know how I can acces to the content items?
sure, added that as well
great! thanks just one final question?! can you please also let me know how to print all items(looping through all items in array and listing them)?
What do mean by "print"? In the console?
I mean displaying all items in the content
|
0

You content variable is an object that contains:

item1: String
item2: String
item3: array

var content = {
  item1: "John",
  item2: "Doe",
  item3: { "item3_1", "item3_2", "item3_3"   }
}

If item3 is an array then is should use the array notation

var content = {
  item1: "John",
  item2: "Doe",
  item3: ["item3_1", "item3_2", "item3_3"]
}

Now you can pass this object around and call its fields like that:

function print(obj) {
  var fname = obj.item1;
  var lname = obj.item2;
  var array = obj.item3;
  alert(array.toString());
}

where call to the function will look like

print(content);

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.