-1

I'm attempting to create a JavaScript array and add objects to that array. I'm looping through the fields in a page and creating an object to add to the array at that time (see code) but for some reason when creating a new object the whole page of javascript code stops working. Am I doing something wrong in creating the object? Should I be doing it differently?

When I remove the "FArray[i] = {}" code and the rest of the code in the page starts working again.

function SetFields()
{
      var FArray = new Array(FieldSet.length);
      for (var i = 0; i < FieldSet.length; i++)
      {
            FArray[i] = { ID = FieldSet[i].ID, Value = document.getElementById(FieldSet[i].ID).value };
      }
      alert(FArray.length);
}

var FieldSet;
1
  • FieldSet.length === 0, so loop never runs. Event if FieldSet has a length it would have to be an Array of Objects, each Object having an ID property... not to mention that is not proper Object syntax anyways. Commented Apr 23, 2019 at 23:21

1 Answer 1

4

When you create an object you should use a colon not an equals sign.

e.g.

let myObj = {id: 1, value: 2}
Sign up to request clarification or add additional context in comments.

1 Comment

It's the small things in life, it looks like that was it.

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.