obito M
obito M

Reputation: 25

How to get useful information from console.log()

I am using OpenLayers and jQuery to map a GeoJson file with some features and their properties

My goal is to get the list of properties of a feature (named my_feature).

So, I tried the code below :

    var list_of_keys = Object.keys(my_feature.getProperties());
    $('<input>').val(list_of_keys).appendTo('body').select();
    document.execCommand('copy');

When this is performed, the list is successfully copied. And with a (cntl + v), I get :

geometry,Name,refer_desc,refer_de_1,refer_TNZM,refer_TNZV,refer_TNZI,refer_TNZC,refer_MN,refer_MR,refer_MA,refer_AN,refer_AR,refer_AA,refer_VN,refer_VR,refer_VA,refer_PBN,refer_PBR,refer_PBA,refer_de_2

But this result is only copied to the clipboard and not assigned to a variable that I can use later in the code.

In fact, when I am doing this :

    var list_of_keys = Object.keys(my_feature.getProperties());
    var x = $('<input>').val(list_of_keys).appendTo('body').select();
    console.log(x);

I get a weird return that I cannot understand (see image):

enter image description here

So my questions are :

1. Why I am getting different returns with the copy and the console
2. What the return in the console mean 
3. How to get the copied list [geometry,Name,..,refer_de_2] to a variable (for example x = [geometry,Name,..,refer_de_2]) and then use it later in the code

Upvotes: 0

Views: 55

Answers (1)

Sysix
Sysix

Reputation: 1806

You are using the browser behavior to copy the input value. But jQuery select() returns always a jQuery object.

See the docs: https://api.jquery.com/select/

Also execCommand is depreacted, use the new Clipboard API:
https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand

https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/writeText

You need then clip your list_of_keys variable with .join(',')

function setClipboardText(text) {   
    navigator.clipboard.writeText(text).then(
        function () {
        /* success */
        },
        function () {
        /* failure */
        }
    );
}

var list_of_keys = Object.keys(my_feature.getProperties());
console.log(list_of_keys.join(','));

setClipboardText(list_of_keys.join(','));

Upvotes: 1

Related Questions