简体   繁体   中英

Using Object.keys returns integer array

I am using iOS Workflow to pass the following string {"person":"me","age":"30"} to iOS Drafts using Run Drafts Action.

The action executes this Javascript

var txt = draft.content;

// var jsontest = {"person":"me" ,"age":"30"};

jsontest = txt.match(/{.*}/);

var str = JSON.parse(jsontest)

var list = JSON.stringify(str)

// List is {"person":"me" ,"age":"30"};

keylist = Object.keys(list);

keylist = keylist.join("|");  

alert(keylist);

// draft.defineTag('buttons',str);

The result should by person|age but it is 1|2|3|4|… for the length of the string

What am I doing wrong.?

Most of your code is unnecessary. You're ending up with a string of the content between the braces, and asking for its keys , which will be the indices of the string.

If you wanted the keys from the parsed object, then parse it (don't stringify it back to JSON), and use Object.keys on that.

 var jsontest = '{"person":"me" ,"age":"30"}'; var obj = JSON.parse(jsontest); var keys = Object.keys(obj); console.log(keys.join("|")); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM