Creating key/value pairs from two different arrays

Options
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.

Comments

  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options
    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
  • espptyltd
    espptyltd Member
    Options
     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! 
  • Umair Kamil
    Umair Kamil Member
    Options
     - 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]
  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options
     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?
  • Umair Kamil
    Umair Kamil Member
    Options
    

    Basically, the output that I want is this

    object:

    {
    id: 1
    name: Apples
    price: 30
    }

    {
    id: 2
    name: Bananas
    price: 500
    }
    Reply
    Like
  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options

    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.
  • Umair Kamil
    Umair Kamil Member
    Options
     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?  
  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options
     My coaching is a professional practice. You can get current pricing here. 
  • Umair Kamil
    Umair Kamil Member
    Options
     - 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.
  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options
     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? 
  • Umair Kamil
    Umair Kamil Member
    Options
     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
    }
  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options
     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. 
  • Umair Kamil
    Umair Kamil Member
    Options
     - 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]
  • Umair Kamil
    Umair Kamil Member
    Options
     - Just a humble request! The uploaded video unfortunately doesn't have audio. Would be great for anyone's future reference and knowledge. 
  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options
     Yes, Descript let me down in the recording. Will re-cut it. 
  • Umair Kamil
    Umair Kamil Member
    Options
     - 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. 😛
  • Umair Kamil
    Umair Kamil Member
    Options
     - 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. 
  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options
     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.
  • Valentin R
    Valentin R Member
    Options

    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?