79483625

Date: 2025-03-04 12:04:02
Score: 0.5
Natty:
Report link

Brackets do multiple things in JavaScript.

What you want to do is add elements to indexes in the Array object called fileData. Brackets can be used to add elements to indexes.

Because in JavaScript an Array is a descendant of an object, you can actually add properties to it as well. If

data["uniquePropertyName"]

were equal to something like 3, bracket notation would allow you to make an assignment to fileData[3].

If however, data["uniquePropertyName"] makes reference to something like a string, you will create a property on fileData.

let array = [];

console.log(typeof array);
//OUTPUT: object

let data = { my_object_key: "my object value", value: "my 2nd object value" };

array[data["value"]] = "something that I am trying to insert into array";

console.log(array);
//OUTPUT: [ 'my 2nd object value': 'something that I am trying to insert into array' ]

console.log(array['my 2nd object value']);
//OUTPUT: something that I am trying to insert into array

array[0] = "Another array insertion";
array[1] = "2nd array insertion";
array[2] = "Third array insertion";

console.log(array);
//OUTPUT:
// [
//   'Another array insertion',
//   '2nd array insertion',
//   'Third array insertion',
//   'my 2nd object value': 'something that I am trying to insert into array'
// ]

But if data["uniquePropertyName"] makes reference to an object:

let evil_deed = { do_not: { try_this: "at home" } };

array[evil_deed["do_not"]] = "Why, you ask?";

console.log(array)
//OUPUT:
// [
//   'Another array insertion',
//   '2nd array insertion',
//   'Third array insertion',
//   'my 2nd object value': 'something that I am trying to insert into array',
//   '[object Object]': 'Why, you ask?'
// ]

That's all fun and games, until you are trying to access that property:

console.log(array[evil_deed["do_not"]])
//OUPUT: Why, you ask?

In the second example

You are creating an object with a single property name, and then pushing that object into an Array. That will place the elements into indexes.

Reasons:
  • Blacklisted phrase (1): I am trying to
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Jay Harrison Crawford