0

Hi I have use the following code snippet to create the array .

var rows = [];            
rows["_id1"] = 1;

but in this case 1 did not insert into the array. Is there any other way to achieve this.

Screenshot:

enter image description here

enter image description here

5
  • It definitely did get turned into a property on the array, but that’s not a feature specific to arrays. What are you trying to accomplish? Commented Nov 14, 2013 at 4:03
  • As you can see, you didn't insert it into the array, instead you made a property. Commented Nov 14, 2013 at 4:04
  • what result do you expect? Commented Nov 14, 2013 at 4:04
  • I want to rows as like newly attached screen shot in that i want id0 instead of 0 .is there any other way to achieve this Commented Nov 14, 2013 at 4:10
  • Arrays in JavaScript are numeric only, there are no associative arrays. What you did was set a property of the array, you did not "insert" into it. Commented Nov 14, 2013 at 4:15

1 Answer 1

5

Make it an object.

var rows = {};
rows["_id1"] = 1;

Or if you really want an array, you can have an array of objects

rows.push({"_id1": 1});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help, in this case how to use the indexOf option from rows . i have tried with but it always return -1 as index
What are you trying to accomplish? If you use an object, you won't be able to find the index. Are you trying to see if something is in rows? If so, you can just do if (rows["_id1"])...

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.