Convert Object from grabAttributeFrom to array

hi all,

I’m using grabAttributesFrom to get the data from a ‘data-options’:
let arrayOfHardwareColour = await I.grabAttributeFrom(masters.fieldsOrder.doorHardwareColour, 'data-options');

And this is returning me an object like this:
[{"id":"0","text":"None"},{"id":"1","text":"Black"},{"id":"4","text":"Bright Chrome"},{"id":"3","text":"Brushed Steel"},{"id":"5","text":"Gold"},{"id":"7","text":"Satin Silver"},{"id":"8","text":"Smokey Chrome"},{"id":"9","text":"White"}]

when I check:

I.say(Object.values(arrayOfHardwareColour));
result:
[{"id":"0","text":"None"},{"id":"1","text":"Black"},{"id":"4","text":"Bright Chrome"},{"id":"3","text":"Brushed Steel"},{"id":"5","text":"Gold"},{"id":"7","text":"Satin Silver"},{"id":"8","text":"Smokey Chrome"},{"id":"9","text":"White"}]

I.say(Object.keys(arrayOfHardwareColour));
result:
0

I.say(arrayOfHardwareColour[0]);
result:
[{"id":"0","text":"None"},{"id":"1","text":"Black"},{"id":"4","text":"Bright Chrome"},{"id":"3","text":"Brushed Steel"},{"id":"5","text":"Gold"},{"id":"7","text":"Satin Silver"},{"id":"8","text":"Smokey Chrome"},{"id":"9","text":"White"}]

Someone know how can I browse on this object? I mean, I’ve already tried a “for…of” but this just gives me back one record… ‘toString.split’ … ‘Object.keys’ … In many different ways… Does someone have any suggestion?

Hi, have you tried JSON.parse() ?

Yeah, @ufko ! :frowning_face:

Result:
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

Oh :frowning: I had a similar issue. To solve it I first JSON.stringify’ed the object with the below:

const alreadySerialized = [];
const myNiceObject = JSON.stringify(mySadObject, (key, value) => {
        if (value !== null && typeof value === 'object') {
            if (alreadySerialized.indexOf(value) >= 0) {
                return;
            }
            alreadySerialized.push(value);
        }
        return value;
    });

@ufko
When I used JSON.stringify, I’ve already reached the bad result and similar when I tried to use your method above.
Result:
["[{\"id\":\"0\",\"text\":\"None\"},{\"id\":\"1\",\"text\":\"Black\"},{\"id\":\"4\",\"text\":\"Bright Chrome\"},{\"id\":\"3\",\"text\":\"Brushed Steel\"},{\"id\":\"5\",\"text\":\"Gold\"},{\"id\":\"7\",\"text\":\"Satin Silver\"},{\"id\":\"8\",\"text\":\"Smokey Chrome\"},{\"id\":\"9\",\"text\":\"White\"}]"]

When I changed the result of your method:

 if (alreadySerialized.indexOf(value) >= 0) {
          return;
        }
        alreadySerialized.push(value);
      }
      return alreadySerialized;

the result is:
[null]

I think I’ll build a parser for that…

:frowning: good luck!

@andreyagra this is already an array, look at the surrounding […]

do you have any example repo to reproduce this?

@paulb… This seems with an array, but it isn’t… :slight_smile: this is an object

His behavior is different… if you try to browse into it, you’ll just find only one item… there is no key, value…

so, I’m having to convert this ‘String’ to JS Object… And so, I have to browse into two array levels to reach the value…

When I finalize this method, I’ll post here hoping someone will suggest something better…

1 Like