Dynamically get id of items

Yuuta Kirishima

Software Engineer/Professional Mayonnaise Player
Veteran
Joined
Jul 9, 2013
Messages
550
Reaction score
131
First Language
Engilsh
Primarily Uses
RMMV
I have a for loop that pushes the ids of the items in the array into a new array, however Ruby is different from JavaScript and I can't seem to figure out how to accomplish this, here's how i did it in ruby:

Code:
for items_array.each do |i|
     new_array.push($data_items[i].id)
end
what is the equivalent in MV?
 

Llareian

Jack of All Trades, Master of None
Veteran
Joined
Jan 26, 2017
Messages
603
Reaction score
1,400
First Language
English
Primarily Uses
RMMV
I know JavaScript but not Ruby, so I'm having trouble reading what the function you've placed here is actually doing. It seems to be creating new_array by calling from the database, but I'm not sure what items_array is or where i comes from. Can you clarify what you need the function to do?

EDIT: To clarify some of my confusion, in MV's JavaScript,
Code:
$dataItems[i].id
returns i. So to me (without knowing Ruby) it looks like you're taking an array of item ids and making that same array again, but by filtering it through the database for some reason. Also, there are TONS of array functions you can use to pull useful information from an array of actual item objects, so I really need to know what you're trying to accomplish and what you're starting with.
 
Last edited:

Sarlecc

Veteran
Veteran
Joined
Sep 16, 2012
Messages
454
Reaction score
209
First Language
English
Primarily Uses
RMMV
I believe the equivalent for what you have is a for...in loop, in JavaScript for...in loops are not recommended for arrays (they are useful for objects though).
Code:
//Here is a JavaScript version using a normal for loop
//Assuming itemArray and newArray are already defined variables
for (var i = 0; i < itemArray.length; i++) {
    newArray.push($dataItems[i].id);
}
 

Yuuta Kirishima

Software Engineer/Professional Mayonnaise Player
Veteran
Joined
Jul 9, 2013
Messages
550
Reaction score
131
First Language
Engilsh
Primarily Uses
RMMV
@Llareian @Sarlecc Thank you both I'm pretty dumb for some reason I thought $dataItems.id wouldn't work when in fact it does.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

I feel like it's about time I share all the result of my experiment project here. But there's a lot of it.
Me and brother have an ongoing rivalry. We constantly fight for the High Score in Galaga on our arcade cabinet. Today I take back the top with 239,000. He'll be back. (No-Fire glitch is banned)
Made Custom ENEMY Information Screen
I'm having such a hard time thinking of an interesting battle system. and not being a coder, certain things I wanna do seem a lot harder lol.
Created a character outline with some code. I have no use for this. I'm....bored.....

Forum statistics

Threads
93,721
Messages
914,920
Members
123,324
Latest member
ahmbor
Top