2

As I know, it is possible to push more data into an array. Fe, I have an array:

G = [12, 34, 5]. 

Right now, I can access the nth element like this:

G[n]

I'd now like to push new data in it with a label, so I want the array to look like

G = [12, 34, 5, label:567856, other: Infinity]

where I can get 567856 by calling

G["label"] //(or Infinity by calling G["other"]). How can I achieve this? 

I've found

G[i].push({
    label:567856, 
    other: Infinity
})

but this way it adds it as a whole new element, and I'm only able to call G[4]["other"], instead of G["other"]. How can I add the element as I've described?

Thank you!

4
  • Arrays does not have labels, you need to use another datatype. Commented Jan 29, 2016 at 22:12
  • That is not possible. Probably you want to use JSON objects instead Commented Jan 29, 2016 at 22:12
  • Is it important to your needs treating this new labelled data as a new element? Couldn't it be just a property, and call like G["other"]? In fact you could use a sort of prototype to handle this, but I'd suggest you to keep things simpler, and avoid spending too much effort on small things. Commented Jan 29, 2016 at 22:35
  • Hi @lte__, please let me know if my answer use full for you ( so i could update my answer..) ? Commented Feb 1, 2016 at 10:32

4 Answers 4

2

To add onto Andriy's answer, you need to use Javascript Objects rather than arrays. An object can have indices with custom names. For example, given

var newObj = {"hello": "world", "value":1, "inf": Infinity}

you can do

newObj['hello'] // "world"
newObj['value'] // 1

The problem with

G[i].push({
    label:567856, 
    other: Infinity
})

is that you are pushing an object with 2 attributes, not pushing 2 objects, that's why you need to use G[4]["other"]

See running JSFiddle example.

Sign up to request clarification or add additional context in comments.

Comments

1
G["other"] = "something";

With this you will keep the original array, and now have the attribute other, but it is not in [12, 34, 5]

Whit this one you can add an object to the array:

G.push({other: 123})
console.log(G);//[12, 34, 5, object]
console.log(G[3].other);//123

The problem with

G[i].push({
    label:567856, 
    other: Infinity
})

is that you are pushing an object with 2 attributes, not pushing 2 objects, that's why you need to use G[4]["other"]

Comments

0

Arrays in JavaScript are a type of object. As such, they can contain properties:

G.label = 567856;
G.other = Infinity;

The advantage of arrays over other objects is that their indexed elements are ordered.

If you'd like the fourth and fifth elements in the array to be 567856 and Infinity and you want to be able to refer to those values with G.label and G.other, you can do so as follows:

var G = [12, 34, 5];

G.push(G.label = 567856);     //same as G.label = 567856;  G.push(G.label);
G.push(G.other = Infinity);

You can still iterate through the array using a loop:

var G = [12, 34, 5];

G.push(G.label = 567856);    
G.push(G.other = Infinity);

G.forEach(function(val) {
  console.log(val);  // 12 ... 34 ... 5 ... 567856 ... Infinity
});

console.log(G.label);  //567856
console.log(G.other);  //Infinity

Note that this does create duplicates. If you change G.label or G.other afterwards, those changes will not be reflected in the fourth and fifth elements of the array.

However, you can overcome that by creating setters on G.label and G.other using Object.defineProperty():

var G = [12, 34, 5];

G.push(G.label = 567856);
G.push(G.other = Infinity);

G.forEach(function(val) {
  console.log(val); // 12 ... 34 ... 5 ... 567856 ... Infinity
});

console.log(G.label); //567856
console.log(G.other); //Infinity

Object.defineProperty(G, 'label', {
  set: function(x) {
    this[3] = x;
  }
});

Object.defineProperty(G, 'other', {
  set: function(x) {
    this[4] = x;
  }
})

G.label = 99999;
G.other = 11111;

G.forEach(function(val) {
  console.log(val); // 12 ... 34 ... 5 ... 99999 ... 11111
});

Comments

0

Arrays isn't designed to suit your case.

See Array element accessing flow from ECMAScript 262, 5.1 15.4

Array objects give special treatment to a certain class of property names. A property name P (in the form of a String value) is an array index if and only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 2^32−1.

So you simply cannot access Array element by alphabetical name because that key won't be parsed to integer by ToUint32.

You can add object to array and store it's index after pushing into array ( Array.prototype.push would return you size of your array):

var G = [1,3,4];
var labelIndex = G.push({'label': 123}) - 1;
console.log(G[labelIndex]["label"]);

Actually that's solution would suite case when you have two or more objects inside your array with same property.

Suggestion below not recommended!

However, you can use code below to define your G Array properties, but it's not value of property of item from your array, it's array property:

G.other = Infinity;
G.label = 567856;
// Access newly created properties
console.log(G["other"]);
console.log(G["label"]);

Good Luck !

1 Comment

This does not add a new element in the array

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.