Creating key/value pairs from two different arrays
Options
espptyltd
Member ✭
I am looking to combine two different arrays that do not contain key/value pairs, into one single object that contains key/value pairs.
I would like the values in the first array to become the keys, and the values from the second array to become the key's from the first array's values.
The order of each item in each array dictates the matching criteria, so for example, item 1 from field_ids should become the key of item 1 from field_values. Therefore the outcome as per my example would be
"project_name":"Params project test"
I have attached the source JSON, and what I am hoping I can achieve post the transformation
[SeparateArrays.png][KeyValuePairs.png]
I wasn't sure how to go about referring to a list item's position in order to bring the items from the two separate lists that are in the same position in their respective lists together, and additionally I'm not sure of how to create a key from one item and a value from another item.
Thanks for any help.
I would like the values in the first array to become the keys, and the values from the second array to become the key's from the first array's values.
The order of each item in each array dictates the matching criteria, so for example, item 1 from field_ids should become the key of item 1 from field_values. Therefore the outcome as per my example would be
"project_name":"Params project test"
I have attached the source JSON, and what I am hoping I can achieve post the transformation
[SeparateArrays.png][KeyValuePairs.png]
I wasn't sure how to go about referring to a list item's position in order to bring the items from the two separate lists that are in the same position in their respective lists together, and additionally I'm not sure of how to create a key from one item and a value from another item.
Thanks for any help.
Comments
-
Cool problem! I made a quick video to walk through this process based on my understanding of your situation. Let me know if it helps? https://www.loom.com/share/78245355b2814ed099b63a9c2bf317f1
-
Amazing stuff! Thank you once again for the detailed video walkthrough of the challenge I was facing.
I wasn't aware of the set filter, which is exactly what I was looking for.
Xano is incredible! -
- Hope you are well! I have run into a problem similar to this.
Objective: I want to create key-value pairs from multiple arrays
Here is where I am so far:
[Screen Shot 2022-06-09 at 18.16.38 PM.png][Screen Shot 2022-06-09 at 18.16.51 PM.png] -
What do you want to be the keys and what do you want to be the values? I would loop over the common index and build out the objects, like I show in the video. Is there a reason you are going a different way?
-
Basically, the output that I want is this
object:
{
id: 1
name: Apples
price: 30
}
{
id: 2
name: Bananas
price: 500
}
Reply
Like -
great! I think my vid should upthread should help. If you want to schedule a 1-1 coaching session to work this problem, I’d be glad to help that way as well. -
Thank you! I will sign up for the coaching session!
My original objective is/was to allow the user to be able to bulk update records from the front end.
For this, I have a few quick questions:
• Are you referring to the video you have shared in this thread? If so, I have watched it several times and I understand that you are creating an index based on the number of keys and then assigning all the values to that object.
• As the user is inputting a list of ids, names and prices, would the best approach be to convert this into an object with multiple items? Or is there any other better approach?
• Just wanted to confirm, is the coaching paid or free? -
My coaching is a professional practice. You can get current pricing here.
-
- Great! But could I humbly request some quick guidance so that I can build some skill in Xano? I have only just started a couple of days ago and would like that I have at least a few solid problems to hand before I schedule a coaching session.
-
The video seems like a pretty direct application to this immediate problem. Did you view it already? Is there a particular aspect of the problem that is particularly opaque that I might be able to help with?
-
Hi Ray, let me explain my confusion. The video is based on 1 key and 1 value pair. In my situation, the user is entering multiple values as lists. So for example, a simple table of people and their ages:
Keys_Input []
Name_Values []
Age_Values []
So the keys are name, age and I want to make sure that for each value of name and height, there is a separate item. Therefore, the result I end up being stuck with is:
{
name: "Ray", "Elia"
age: 30, 28
}
BUT the result I want is:
{
name: Ray
age: 30
}
{
name: Elia
age: 28
} -
I see the confusion. You have parallel arrays of values, while he had parallel arrays of keys and values. So what you want is to result in an array of objects. Here's a video (RHD: fixed the link) walking through what I think will help.
-
- Thank you so so much for this! I've managed to get it working and just wanted to add:
• This originally started from "allowing the user to make bulk updates/allowing the user to edit multiple records" at once
• After watching the video by on "External APIs: Adding multiple records from an external API", I immediately realized that although this works for an external API where the response includes the array and objects, it would not work for my situation where the user or external API intends to "edit multiple records" in my Xano Database. This is especially true because I want to give the user/front-end the flexibility to input data as a list!
• Thereafter, it struck me that I need to combine the user's inputs into an Object BUT I was really confused about the differences between an "Array of Objects" and a "Single Object"
• So I started searching "object" and "array" in both, YouTube and the Xano community which landed me here
• The before solution
[before_1.png][before_2.png][before_3.png]
• The after solution
[Screen Shot 2022-06-10 at 16.37.06 PM.png][Screen Shot 2022-06-10 at 16.37.19 PM.png] -
- Just a humble request! The uploaded video unfortunately doesn't have audio. Would be great for anyone's future reference and knowledge.
-
Yes, Descript let me down in the recording. Will re-cut it.
-
- No worries! Regardless, your explanations are so amazing that that let down (by descript) wasn't a big issue after all! Just had to think harder at each step. 😛
-
- Do let me know whenever you update the video. Just want to make sure that I haven't missed out on any concept, as I will certainly be referencing this video to help others in the community. My next step is to work with object_inputs from the user and I am sure this knowledge will come very handy in that.
-
Here you go - the video with audio. I also edited the post upthread to use the link to this new vid so others don't get confused.
-
Similar question, but about a JSON.
Let's say I have these as inputs:{
"a": 1,
"c": 2,
"e": 3
}
And then I get some data that looks like this:
{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5,
}
Is there a way to update my matching inputs with the values that I got in the second json?
Categories
- All Categories
- 53 ? Announcements
- 47 ? Releases
- 37 ? Welcome
- 983 ? Help! I'm a Noob
- 125 ? No-Code Front-Ends
- 633 ? Working with APIs
- 439 ? Transforming data
- 126 ? Connect Xano to ...
- 50 ?? Find an Expert
- 348 ❓Other questions
- 35 ? Security
- 22 ✂️ Snippets
- 19 ? Showcase
- 7 ?️ Xano Chatter
- 62 ? Video Tutorials
- 171 ? Request a feature
- 229 ? Report a Bug
- 19 ? Templates & Extensions
- 7 ? Feedback