1

enter image description hereI need to insert a file object at specific index like:1,5,6 in an empty array using angularJs. I want result something like given in the following attached image

enter image description here

1 Answer 1

1

You can just use the [n] notation.

const myArray = []

myArray[1] = 'something'
myArray[5] = 'inserted'
myArray[6] = 'in array'

console.log('myArray ', myArray)

Although I would recommend using javascript object if you need to work with custom property names.

Then you would do something like this:

const myObject = {}
myObject[1] = 'first file'
myObject[5] = 'second file'
myObject[6] = 'third file'

and you can always iterate through that object like this:

Object.keys(myObject).forEach(propertyName => console.log('property name ', propertyName, ' property value ', myObject[propertyName]));
Sign up to request clarification or add additional context in comments.

2 Comments

It is working for only first insertion and not after that
What do you mean?

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.