Object.entries supported in Wappler? - Trying to convert object to array

Hi all,

I’ve found a couple of articles that explain how to onvert an object to an array simply.

Both suggest something like this:

Object.entries(numbers);
// [ ['one', 1], ['two', 2] ]

and

var obj ={"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10"‌​:0,"11":0,"12":0} 
Object.entries(obj);

replacing ‘numbers/obj’ with my object.

However, whenever I try this, or other variations found online, I get an error:

"Parser Error: Formatter "entries" does not exist, expression {{object.entries(h)}}"

Wappler does have the keys and values formatters, which appear to be an older version of what was available for getting keys / values from an object and turning them into an array. What I’m trying to do is get the full key pair.

UPDATE:

Got a bit further by creating a custom formatter:

// JavaScript Document

exports.objecttoarray = function (obj) {
    if (typeof obj != 'object') return obj;
    return Object.entries(obj);
};

However, it’s returning key pairs for every item within the object, which isn’t ideal - if any JS experts might have any ideas, I’d be grateful!

What is the ideal output? And the original array input?

Hey Sid, the ideal output is one single array, that contains the correctly structured schema within it, of objects, arrays and values. That way I can use some of the Data Transformations Wappler has created.

Can you please share an example of input and output? Not really following what the desired operation here is.

Hey Sid, of course:

So you have this api response:

data: {
userid: 5
name: john smith
location: san francisco
}

What I have working is a conversion that does this:

data: [
[0] 
     [0] userid
     [1]5
[1]
     [0] name
     [1] john smith
[2] 
     [0] location
     [1] san francisco
]

Instead I would like

data: [
userid: 5
name: john smith
location: san francisco
]

I tried it on the server action side, and here’s what I could come up with:

Created a set value with dummy JSON data.
Created another set value with junk data & save.
Open the SA in code view and change the value of second set value as shown.

This is the output:
image

There is an issue since past two/three version with the server side SET VALUE and custom text/html/bindings etc, so you cannot add ["{{data}}"]directly in the UI.
Although, in this particular case, I don’t think set value support being set as an array like, generally. So code view is the only option.

Thanks Sid, this looked like it might do the trick but unfortunately none of the fields from the data source are available in the next step for the Filter Columns data transformation…

You can use ["{{data}}"] from @sid or use "{{[data]}}" which does exactly the same. Wappler is not smart enough to get the metadata from an expression that manipulates data, it only knows for the steps where it is exactly defined. Perhaps @George could improve that in the future. So for the steps that use that value you have to write the expressions by hand instead.

2 Likes

Thanks Patrick. I’ll persevere with this, and take a look at how the Filters work with a normal array to see if I can replicate.