Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to push elements to an array #285

Open
AnupKumarPanwar opened this issue Feb 23, 2023 · 6 comments
Open

How to push elements to an array #285

AnupKumarPanwar opened this issue Feb 23, 2023 · 6 comments
Labels
support Users asking how to solve a specific issue

Comments

@AnupKumarPanwar
Copy link

AnupKumarPanwar commented Feb 23, 2023

I want to keep a track of visited productIds in an array. How do I push elements to an array?

I've tried the following code but it is not working.

let visitedProductIds = [1,2,3]
$visitedProductIds = $visitedProductIds + [4,5,6]

{
    "visited": $visitedProductIds
}

@larsga larsga added the support Users asking how to solve a specific issue label Feb 23, 2023
@larsga
Copy link
Collaborator

larsga commented Feb 23, 2023

You can't. This is a functional language, so you can't change values. You can build new ones, but you can't change the ones that already exist.

You need to do something like this:

{
    "visited": <expression that computes visited product ids>
}

Now I don't understand what "visited" means in this context, but maybe something like this?

let visitedProducts = <whatever you do to make that list>

{
  "visited" : [for ($visitedProducts) .id], // or something like that
  "products" : $visitedProducts, // or something like it
}

I hope this helps.

@oyeyemi
Copy link

oyeyemi commented May 20, 2023

Hello, i am newbie in jslt. I don't know how to transform this type of array of json in jslt. Please i would need your help.
My input json is :
{
"test1" : "test output1",
"Details" : {
"0" : {
"id" : "first"
},
"1" : {
"id" : "second"
}
}
}

And my desired output is this.
{
“Details” : {
“0” : {
“id” : “first”,
“test1" : “test output1”
},
“1" : {
“id” : “second”,
“test1” : “test output1"
}
}
}

@larsga
Copy link
Collaborator

larsga commented May 20, 2023

This is a new question, so it would be better to make a new issue for it. I'll answer you here anyway.

It's a bit difficult to tell what rules you want to follow here, but I'll make a guess. Maybe like this?

let obj = .
{
  "Details" : {for (.Details)
    .key : .value + {"test1" : $obj.test1}
  }
}

@oyeyemi
Copy link

oyeyemi commented May 20, 2023

Thank you so much for answering my question despite posted to the wrong place.
I noticed this part (+ {"test1" : $obj.test1}) did the trick. Please, can you kindly explain it to me in brief.
Thanks for your help.

@larsga
Copy link
Collaborator

larsga commented May 20, 2023

Using + with two objects merges the two objects into one. Try it out in the playground and you'll see.

@oyeyemi
Copy link

oyeyemi commented May 20, 2023

Thank you for the explanation. I am on playground already. I also noticed it's appending comma(,) separator automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Users asking how to solve a specific issue
Projects
None yet
Development

No branches or pull requests

3 participants