Array Insert at index not working

Wappler Version : 2.4.3
Operating System : Mac OSX Mojave v10.14.5

Hi all,

Clicking on a button must insert “Test” into arr1 at index 111. Instead it’s inserting at index 0.

Fundamental thing, not working :neutral_face: Any workaround until this is fixed? This is a big blocker, any help will be appreciated. Thanks!

Hello,
Are you trying to insert an item at a specific index in an empty array?
I just tested this and it works as expected, when the array has items. What’s the point of setting a specific index in an empty array?

Hi @Teodor,

image

Are you saying “Insert Item” is in fact a way to update only an existing item at a particular index in an array?

There are various use cases where we want to insert (or) update an array item at a particular index. Don’t want to go into too much detail, but just want to make sure I understand what this function is.

Insert item inserts the value at the selected index in the array.

Replace Item At updates the existing value at the selected index in the array.

Can you please explain when would you need to insert an item at a specific index in an empty array?

Ahhh… okay… i think i understand now… What i’m after is keyed arrays, will try that. thanks!

@Teodor @patrick , can you advise if I can use the array or variable components (client-side) to build an array of objects (not sure if this is what is called a “keyed array”)?

What I want is something like this at the end:

arr1 =  
[ 
    { "order1" : {"productid" : "121",   "producttype": "metal"}},
    { "order2" : {"productid" : "122",   "producttype": "paper"}},
    { "order3" : {"productid" : "123",   "producttype": "rock"}},
    { "order4" : {"productid" : "124",   "producttype": "scissor"}}
]

and I want to access each object like this

obj1 = arr1.items["order1"]
obj2 = arr1.items["order2"] 
obj3 = arr1.items["order3"]

and display the object properties further like this in para elements -

obj1.productid
obj1.producttype

Basically, I want to not use “index” but rather use “key”.

Really lost here! Should I be using the custom formatter that @George referred to in his post here How to handle translations, site w multiple languages

<script>
dmx.Formatters('array', {
		toKeyedObject: function(array, key, value) {
			var newObj = {};
			for (var ai=0; ai < array.length; ai++) {
				newObj[array[ai][key]] = array[ai][value];

			}
			return newObj;
  }
 });
</script>

@George, are you able to help with the toKeyedObject formatter please. Looks like it’s your baby. See the scenario I have explained above for details. Thanks in advance.

@Akayy
Just a friendly reminder:
It’s Saturday evening and our office opens on Monday morning. Don’t expect immediate help during the weekends, as we are not officially working then. We do help, when we have free time and we monitor the forums.

George as well as the rest of the team are probably enjoying the time with their family during the weekends.

Tagging everyone in your topics every few hours won’t really speed up answering your question.

Anyway, you will get answers to your questions when we are back in the office.

2 Likes

Apologies! All good :slight_smile:

1 Like

I figured out how toKeyedObject formatter works. Thanks to jeoff’s video, lifesaver - How to handle translations, site w multiple languages

This formatter opens up soo many wonderful possibilities… Cheers!