question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nested array within an Array Object

See original GitHub issue

I was hoping to figure out how I can have an array or an array object within another array object.

my desired output

{
  "staff": [{
    "name": "john doe",
    "another_array": [
        "something",
        "something2"
    ],
    "array_obj": [{
         "title": "something",
         "desc": "foobar"
    }]
  }]
}

I’ve tried something like

<input name="staff[]another_array[]">

and something like…

<input name="staff[]array_obj[][title]">

But none of the above works. It doesnt appear you can have nested arrays. Is this true?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
marioizquierdocommented, Nov 26, 2018

Using the array[][nestedObjKey][] syntax works, but it will add keys to the object only if the key does not exist, and create new object elements if the key does not exist… Kind of magic behavior that may work great or be super-confusing.

To avoid confusion, I recommend explicitly specifying the array indexes (use [0], [1], [2], … instead of just []):

<input type="text" name="staff[0][name]" value="john doe" />
<input type="text" name="staff[0][another_array][0]" value="something" />
<input type="text" name="staff[0][another_array][1]" value="something2" />
<input type="text" name="staff[0][array_obj][0][title]" value="something" />
<input type="text" name="staff[0][array_obj][0][desc]" value="foobar" />

And then use serializeJSON with the option useIntKeysAsArrayIndex:

$('input').serializeJSON({useIntKeysAsArrayIndex: true});

This way you get exactly what you would expect:

{
  "staff": [{
    "name": "john doe",
    "another_array": [
        "something",
        "something2"
    ],
    "array_obj": [{
         "title": "something",
         "desc": "foobar"
    }]
  }]
}
0reactions
Windman1320commented, Oct 9, 2018

@randallmlough If you don’t mind ,would you like to tell me how can you get that result ? My output in Chrome is not tidy as your result.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How does Nested Array work in JavaScript? - eduCBA
Nested Array in JavaScript is defined as Array (Outer array) within another array (inner array). An Array can have one or more inner...
Read more >
Understanding Nested Arrays in JavaScript
JavaScript lets us create arrays inside array called Nested Arrays. Nested Arrays have one or many arrays as the element of an array....
Read more >
Group objects inside the nested array JavaScript - Tutorialspoint
Our job is to prepare an object with key as key of objects and value being an array that contains all the values...
Read more >
Understanding Nested Arrays in JavaScript | by Sanchitha SR
Understanding Nested Arrays in JavaScript ... An array is an ordered collection of values: each value is called an element, and each element...
Read more >
How to access a nested array of objects in JavaScript | Code
You can access a nested array of objects either using dot notation or bracket notation. JavaScript has only one data type which can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found