-2

I know that there are 2 ways to set an Object Property as follows.

Obj1.age = 29;
Obj1[age] = 29;

For array I know only 1 way.

arr[3] = 29;

But I feel that there must be other ways of setting values in array. Can someone let me know any other way (except this one)?

Also, for object you know any other way, let me know. Though I think there are only aforementioned 2 ways to set data in properties.

Deadpool
  • 7,811
  • 9
  • 44
  • 88

4 Answers4

2

Let us clarify something first: There is no concept of "values" at the syntax level. You can write

Obj1.age = 29;

no matter what the actual value of Obj1 is. Of course whether or not the assignment will work at runtime will depend on the actual value. But syntactically this code is correct.

The reason we only use bracket notation (arr[0] = 42) when dealing with arrays is not because the value is an array but because this is the only valid syntax when using numbers as property names. The actual value of arr is irrelevant.

Here I'm using an object, but dot notation is still invalid:

var obj = {};
obj.0 = 42;

Bracket notation works:

obj[0] = 42;

See: JavaScript property access: dot notation vs. brackets? for more info.


However, because arrays are just objects, you can assign/access any other property to an array. E.g. a common way to "truncate" an array is to assign a new value to .length:

var arr = [1, 2, 3];
arr.length = 0;
console.log(arr);

Aside from syntax, objects and arrays have a couple of methods to mutate properties:

You can use Object.defineProperty on any object:

var arr = [];
Object.defineProperty(arr, 0, {value: 42, enumerable: true, configurable: true});
console.log(arr);

Arrays specifically also have the following mutation methods: copyWithin, push, pop, splice, unshift.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
1

No, there is no other way for arrays, as arrays don't work with the key-value concepts like objects, they don't have a unique identifier (which objects do: the key). They are only callable via their index, which you access like arr[index].

Also you can define more properties for Objects, using the Object Object (yeah sounds stupid, I know): it has functions like Object.defineProperty, you can find all of them here

Zer0
  • 1,580
  • 10
  • 28
  • *"arrays don't work with the key-value concepts like objects"* That's not true. In fact, arrays are just objects. – Felix Kling Sep 07 '20 at 10:12
  • Well yeah, you can do something like `arr["property"] = 5` and it exists, but it is usually not very advisable – Zer0 Sep 07 '20 at 10:13
1

For arrays, the most convenient way of doing it, and by far the most used, and maybe even the only way. is to set it like arr[3] = 29 i recommend just doing that, and never something else.

For objects, the by far most common way is by doing Obj1.age = 29, tho some people also do Obj1["age"] = 29 (u need the quotes around age unlike what you have in your example). there really is no reason to have/use something else

Luke_
  • 745
  • 10
  • 23
0

This is other way, but your way is cleaner:

arr.splice(3, 0, 29);

But it will shift all the rest elements one position to the right, so if you had elements on position 4, it will become 5th element and so on.

Imbro
  • 479
  • 4
  • 4
  • 1
    No, splice modifies the original value and in this case you insert a new value and are not than modifying it – Zer0 Sep 07 '20 at 10:07
  • first try it, then judge. It works exactly as Deadpool asked. Both insert and modify works (as arr[3] does the same thing) – Imbro Sep 07 '20 at 10:09
  • 1
    I tried it: const a = [1, 2, 3, 4]; a.splice(3, 0, 29); console.log(a); //[1, 2, 3, 29, 4] – Zer0 Sep 07 '20 at 10:11
  • you're right. I'll update my answer. – Imbro Sep 07 '20 at 10:13
  • @Zer0 - you are right, this is no method to add "data" to an array. It only modifies, old array inserting data into it somewhere in between. – Deadpool Sep 07 '20 at 10:26
  • 2
    @Deadpool: You may want to clarify your question, although I think lmbro's example is valid. Your example shows how the 4th element of the array is changed. This example does the same... either way, updating or adding is all the same: It's just property assignment (the property may or may not exist already). – Felix Kling Sep 07 '20 at 10:31
  • @FelixKling - Yes, I think that was my mistake, this thing seems good/valid. – Deadpool Sep 07 '20 at 10:47